Acabo de actualizar mi python de 3.9.1 a 3.9.4. Cuando traté de ejecutar el servidor. La consola me dio una advertencia para esto:
WARNINGS: learning_logs.Entry: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the LearningLogsConfig.default_auto_field attribute to point to a subclass of AutoField, eg 'django.db.models.BigAutoField'. learning_logs.Topic: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the LearningLogsConfig.default_auto_field attribute to point to a subclass of AutoField, eg 'django.db.models.BigAutoField'. No changes detected in app 'learning_logs'¿Puedo saber cómo soluciono esto? Leí la documentación sobre esto, pero no entiendo cómo esta parte de esta página se relaciona con esto.
Modelos.py
from django.db import models from django.contrib.auth.models import User # Create your models here. class Topic(models.Model): text = models.CharField(max_length = 200) date_added = models.DateTimeField(auto_now_add = True) image = models.ImageField(upload_to = 'backgroud_images', null = True, blank = True) owner = models.ForeignKey(User,on_delete = models.CASCADE) def __str__(self): return self.text class Entry(models.Model): topic = models.ForeignKey(Topic,on_delete = models.CASCADE) text = models.TextField() date_added = models.DateTimeField(auto_now_add = True) class Meta: verbose_name_plural = "Entries" def __str__(self): return self.text[:50]Sus modelos no tienen claves principales. Pero Django los crea automáticamente.
Debe elegir el tipo de claves primarias creadas automáticamente https://docs.djangoproject.com/en/3.2/releases/3.2/#customizing-type-of-auto-created-primary-keys (nuevo en Django 3.2)
Agregue esto en settings.py DEFAULT_AUTO_FIELD='django.db.models.AutoField'
o
class Topic(models.Model): id = models.AutoField(primary_key=True) ...Sin querer, ha actualizado Django a 3.2, lo que le advierte y, como texto de sugerencia, sugiere que debe configurar DEFAULT_AUTO_FIELD como se documenta . De esta manera, evitará migraciones no deseadas en versiones futuras, ya que el valor de DEFAULT_AUTO_FIELD se cambiará a BigAutoField
Debe establecer DEFAULT_AUTO_FIELD explícitamente en el valor DEFAULT_AUTO_FIELD actual
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
puede configurarlo incluso por aplicación (si espera crear nuevas aplicaciones con la clave principal de estilo actual)
from django.apps import AppConfig class MyAppConfig(AppConfig): default_auto_field = 'django.db.models.AutoField' name = 'my_app'
o incluso por modelo (desaconsejado)
from django.db import models class MyModel(models.Model): id = models.AutoField(primary_key=True)
También debe cuidar la versión que bloquea sus requisitos, ya que podría introducir cambios incompatibles con versiones anteriores en la producción.
El nuevo archivo settings.py del proyecto creado en django 3.2 incluye esto:
.. # Default primary key field type # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'y como su proyecto se creó en versiones anteriores de django, puede agregarlo a su configuración.