Click here to Skip to main content
15,567,221 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
import uuid
from django.db import models
from django.core import validators

class Author(models.Model):

    class Meta:
        verbose_name = 'Автор'
        verbose_name_plural = 'Авторы'
        ordering = ['id']
        unique_together = ('name', 'age')

    TYPES = (
        ('a', 'forein'),
        ('b', 'domestic'),
        ('c', 'other')
    id = models.UUIDField(primary_key=True, db_index=True, default=uuid.uuid4)
    name = models.CharField(verbose_name='Имя автора', 
     validators=[validators.RegexValidator(regex='^.*em$', message='Wrong')])
    age = models.PositiveIntegerField(verbose_name='Возраст автора')
    email = models.EmailField(verbose_name='Почта автора')
    lit_type = models.CharField(max_length=1, verbose_name='Тип литиратуры', choices=TYPES, default='a')
    def __str__(self):

class Book(models.Model):

    class Meta:
        verbose_name = 'Книга'
        verbose_name_plural = 'Книги'
        get_latest_by = 'published'

    title = models.CharField(max_length=200)
    description = models.TextField()
    page_num = models.PositiveIntegerField()
    published = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

<pre>(venv) PS C:\Users\user\Desktop\dz\new_pr> python makemigrations
It is impossible to add a non-nullable field 'author' to book without specifying a default. This is because the database needs something to populate existing rows.
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
 2) Quit and manually define a default value in
Select an option: 1
Please enter the default value as valid Python.
The datetime and django.utils.timezone modules are available, so it is possible to provide e.g. as a value. 
Type 'exit' to exit this prompt
>>> Traceback (most recent call last):
  File "C:\Users\user\Desktop\dz\new_pr\", line 22, in <module>
  File "C:\Users\user\Desktop\dz\new_pr\", line 18, in main
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\core\management\", line 446, in execute_from_command_line
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\core\management\", line 440, in execute 
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\core\management\", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\core\management\", line 448, in execute     
    output = self.handle(*args, **options)
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\core\management\", line 96, in wrapped      
    res = handle_func(*args, **kwargs)
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\core\management\commands\", line 218, in handle
    changes = autodetector.changes(
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\db\migrations\", line 46, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\db\migrations\", line 195, in _detect_changes
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\db\migrations\", line 1021, in generate_added_fields
    self._generate_added_field(app_label, model_name, field_name)
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\db\migrations\", line 1054, in _generate_added_field
    field.default = self.questioner.ask_not_null_addition(
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\db\migrations\", line 186, in ask_not_null_addition
    return self._ask_default()
  File "C:\Users\user\Desktop\dz\new_pr\venv\lib\site-packages\django\db\migrations\", line 151, in _ask_default

What I have tried:

It doesn’t migrate for me, after I drive 1, it asks for an exit, I press o CTRL + C and that’s it, there is no migration,
Updated 22-Nov-22 22:58pm
Graeme_Grant 22-Nov-22 16:24pm    
The error message is very specific: "It is impossible to add a non-nullable field 'author' to book without specifying a default. This is because the database needs something to populate existing rows."
Eva Smirnova 23-Nov-22 0:18am    
So how should this be fixed?

When you press '1' it should be asking you for the default value to apply.
Ctrl-C will terminate your program without further processing taking place.

So how should this be fixed?
You have several options:
1. Change the database so that the `author` column is nullable - PostgreSQL: Documentation: 7.3: ALTER TABLE[^]
2. Change the database so that a default value is provided when `author` is null - PostgreSQL: Documentation: 15: 5.2. Default Values[^]
3. Or, perhaps the best option, provide an actual value for `author` - you might want to look again at your model and determine why that value is not being returned
Share this answer
Add a default parameter to the name field in your Author class thus:
name = models.CharField(verbose_name='Имя автора', default='no name',
 validators=[validators.RegexValidator(regex='^.*em$', message='Wrong')])
Share this answer
Eva Smirnova 23-Nov-22 6:00am    
The problem was solved otherwise everything worked when I chose 1, then again chose 1 and everything worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900