How to fix Django warning “(models.W042) Auto-created primary key used when not defining a primary key type”?

Your models do not have primary keys. But they are being created automatically by django. You need to choose the type of auto-created primary keys: https://docs.djangoproject.com/en/3.2/releases/3.2/#customizing-type-of-auto-created-primary-keys (new in Django 3.2) Either add this to settings.py: DEFAULT_AUTO_FIELD=’django.db.models.AutoField’ or class Topic(models.Model): id = models.AutoField(primary_key=True) …

Django Static Files Development

Based on what you’ve posted so far, it looks like you’re following the docs for django.contrib.staticfiles. I agree that the docs can be difficult to follow especially if one is new to django. I believe the confusion stems from the fact that django.contrib.staticfiles has two modes of operation: During the development phase where the development … Read more

Django AdminForm field default value

Assuming the value is based on ‘request’ you should use this: class MyModelAdmin(admin.ModelAdmin): def get_form(self, request, obj=None, **kwargs): form = super(MyModelAdmin, self).get_form(request, obj, **kwargs) form.base_fields[‘my_field_name’].initial=”abcd” return form

Django: When extending User, better to use OneToOneField(User) or ForeignKey(User, unique=True)?

The only real reason given in the article is that it can be set up so that the admin page for User will show both the fields in User and UserProfile. This can be replicated with a OneToOneField with a little elbow grease, so unless you’re addicted to showing it in the admin page with … Read more

Django formset_factory vs modelformset_factory vs inlineformset_factory

The difference between the 3 formset factories is basically: formset_factory lets you render a bunch of forms together, but these forms are NOT necessarily related to a particular database models (this is not what you need, since you have models for everything) modelformset_factory lets you create/edit a bunch of Django model objects together, for example, … Read more

What’s the difference between returning a `HttpResponseNotFound` and raising a `Http404` in Django?

An HttpResponseNotFound is just like a normal HttpResponse except it sends error code 404. So it’s up to you to render an appropriate 404 page in that view, otherwise the browser will display its own default. Raising an Http404 exception will trigger Django to call its own 404 error view. Actually, this does little more … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)