django.core.exceptions.FieldError: Cannot resolve keyword 'timestamp' into field

15,929

Solution 1

From the error it should be something like that:

similar_actions = Action.objects.filter(created__gte=last_minute, user_id=user.id, verb=verb)

You're querying for the timestamp attribute of the Action model which does not exist. The available choices are:

created, id, target, target_ct, target_ct_id, target_id, user, user_id, verb

So, you should query the database based on those (or any relation of those) that are attributes of your Action model.

Solution 2

I had the same error when one of columns in my second database was removed.

You can try to reset migrations:

  1. Remove the all migrations files within your project. Go through each of your projects apps migration folder (your_app/migrations/) and remove everything inside, except the init.py file.
  2. Run makemigrations and migrate.

Not sure about your case, but this solved the same error in my situation.

Share:
15,929
Kuba
Author by

Kuba

Updated on June 26, 2022

Comments

  • Kuba
    Kuba almost 2 years

    Welcome friends,

    Unfortunately I have FieldError. Please help

    Environment:

    Django Version: 1.10.6
    Python Version: 3.5.2
    

    utils.py

    import datetime
    from django.utils import timezone
    from django.contrib.contenttypes.models import ContentType
    from .models import Action
    
    def create_action(user, verb, target=None):
        now = timezone.now()
        last_minute = now - datetime.timedelta(seconds=60)
        similar_actions = Action.objects.filter(user_id=user.id, verb=verb, timestamp__gte=last_minute)
        if target:
            target_ct = ContentType.objects.get_for_model(target)
            similar_actions = similar_actions.filter(
                                             target_ct=target_ct,
                                             target_id=target.id)
        if not similar_actions:
            # Nie znaleziono żadnych akcji.
            action = Action(user=user, verb=verb, target=target)
            action.save()
            return True
        return False
    

    models.py

    from django.db import models
    from django.contrib.auth.models import User
    from django.contrib.contenttypes.models import ContentType
    from django.contrib.contenttypes.fields import GenericForeignKey
    
    class Action(models.Model):
       user = models.ForeignKey(User,
                             related_name='actions',
                             db_index=True)
       verb = models.CharField(max_length=255)
    
       target_ct = models.ForeignKey(ContentType,
                                  blank=True,
                                  null=True,
                                  related_name='target_obj')
       target_id = models.PositiveIntegerField(null=True,
                                            blank=True,
                                            db_index=True)
       target = GenericForeignKey('target_ct', 'target_id')
       created = models.DateTimeField(auto_now_add=True,
                                   db_index=True)
    
       class Meta:
           ordering = ('-created',)
    

    I received this error traceback from the Django Shell:

     django.core.exceptions.FieldError: Cannot resolve keyword 'timestamp' 
     into field. Choices are: created, id, target, target_ct, 
     target_ct_id, target_id, user, user_id, verb
    [23/Mar/2017 17:20:46] "POST /account/users/follow/ HTTP/1.1" 500 18933
    

    I received this error from browser console:

    POST http://127.0.0.1:8000/account/users/follow/ 500 (Internal Server Error)
    

    Does anyone coped with this?

    Does anyone have any advise for this?