0

I am using ldap to do authentication, just like Understanding Django-LDAP authentication.

My model is:

class MyFile(models.Model):
    file = models.FileField(upload_to="files")
    slug = models.SlugField(max_length=50, blank=True)
    user = models.ForeignKey(User, unique=True)

But I cannot make migrations:

python src/manage.py makemigrations
You are trying to add a non-nullable field 'user' to myfile without a default;
we can't do that (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)
 2) Quit, and let me add a default in models.py
Select an option: 

Then I input datetime.date.today() and run python manage.py migrate The error is:

  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 915, in get_prep_value
    return int(value)
TypeError: int() argument must be a string or a number, not 'datetime.date'

Any idea? Thanks

UPDATE

class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
    migrations.CreateModel(
        name='MyFile',
        fields=[
            ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ('file', models.FileField(upload_to='files')),
            ('slug', models.SlugField(blank=True)),
            ('user', models.ForeignKey(User, blank=True, null=True)),
        ],
    ),
]

Error:

ValueError: Lookup failed for model referenced by field fileupload.MyFile.user: auth.User
Community
  • 1
  • 1
BAE
  • 8,550
  • 22
  • 88
  • 171

1 Answers1

1

You need to provide a default User ID for all the existing files. It has to be an existing User ID and an integer. It cannot be a datetime.date.

Or, if applicable, you can make the File->User link optional:

user = models.ForeignKey(User, blank=True, null=True)
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • ValueError: Lookup failed for model referenced by field fileupload.MyFile.user: auth.User – BAE Jun 14 '16 at 18:12