1

When I try to add a new field or modify my model in any way I get the following error when I try to migrate to the database:

File "C:\Users\uddin\Envs\web\lib\site-packages\django\db\models\fields\__init__.py", line 1913, in get_prep_value if value and ':' in value: TypeError: argument of type 'datetime.datetime' is not iterable

This is what my model.py file looks like

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
from PIL import Image




class Post(models.Model):
    title = models.CharField(max_length=100)
    title2 = models.CharField( max_length=100)
    content = models.TextField()
    content2 = models.TextField(default=None)
    post_image = models.ImageField(upload_to='post_pics')
    post_image2 = models.ImageField(upload_to='post2_pics')
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    
    
    

    def __str__(self):
        return self.title
    
    def get_absolute_url(self):
        return reverse('post-detail', kwargs={'pk': self.pk})

Full traceback of error

I've added the full traceback in a pastebin as not to make the post too long

front.0008_post_location file

# Generated by Django 3.1.5 on 2021-03-06 01:21

from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

    dependencies = [
        ('front', '0007_post_content'),
    ]

    operations = [
        migrations.AddField(
            model_name='post',
            name='location',
            field=models.GenericIPAddressField(default=django.utils.timezone.now),
            preserve_default=False,
        ),
    ]

Any help would be appreciated

10
  • Show us an insertion example Commented Mar 14, 2021 at 6:35
  • Add the complete error traceback to your question. Commented Mar 14, 2021 at 6:36
  • Do you have or had a GenericIPAddressField anywhere in your models? Commented Mar 14, 2021 at 6:58
  • @AbdulAzizBarkat in my views.py file I've created a view that uses a API to get the IP address of the user and displays the location there at, but there is no GenericIPAddressField in my models. Commented Mar 14, 2021 at 7:04
  • 1
    @MagaBeyworld please show the migration file front.0008_post_location Commented Mar 14, 2021 at 7:05

1 Answer 1

1

The problem is that at some point you added a GenericIPAddressField named location, and gave as the default a callable that returns a datetime. Running the migration raises an exception since a datetime isn't a valid IP address.

When it didn't work, you just deleted the location field. The problem is that Django's migration system records a history of model changes, so removing it just created a new migration (when you ran makemgirations), keeping the bug forever enshrined in this migration file.

Next time, just fix the problem at the time and remove the buggy migration.

For now, you can simply edit the migration to make that line work. Since a later migration deletes the field, it doesn't really matter how you do that. I would simply replace the default with null=True.

Another option would be to use squashmigrations, which might (or might not) remove these redundant migrations that add and then remove a field. Or, if you're still at an early stage in your program, you can reset your migration history. There are various Stack Overflow questions on that subject.

Sign up to request clarification or add additional context in comments.

2 Comments

I've added the file in the post. I don't quite remember using GenericIPAddressField. I know I created a field that was called location that was taking in the IP address using an API which I then deleted because it didn't work.
Thank you for your help!! You have no idea how much headache you've saved me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.