Django: Cannot resolve keyword '' into field. Choices are:
Asked Answered
E

2

8

I am having this weird problem accessing ManyToManyField.

I have following models.

class Link(models.Model):
    title = models.CharField(max_length = 200)
    url = models.URLField(unique = True)
    tags = models.ManyToManyField(Tag)
    creation_date = models.DateTimeField(auto_now_add = True)
    user = models.ForeignKey(User)
    likes = models.ManyToManyField(User, related_name = "%(app_label)s_%(class)s_user_likes")
    dis_likes = models.ManyToManyField(User, related_name = "%(app_label)s_%(class)s_user_dis_likes")

    class Meta:
        abstract = True

class URL(Link):
    preview_image = models.URLField()
    preview_heading = models.CharField(max_length = 100)
    preview_content = models.CharField(max_length = 100)

When I try to access URL.objects.get(pk=1).likes.all(), I get Cannot resolve keyword '' into field. Choices are:... error.

URL.objects.get(pk=1).tags.all(), URL.objects.get(pk=1).user and URL.objects.filter(likes=auser, pk=1) work fine.

Updates:

  1. The fields likes and dis_likes were added using south through schemamigration
  2. Previously I was using Django 1.6.1, updated to Django 1.6.2, the problem still persists
  3. Truncated the database, synced it to have fresh tables, the problem still persists
  4. Partial traceback:

    File "F:\system\env\lib\site-packages\django\db\models\manager.py" in all
      133.         return self.get_queryset()
    File "F:\system\env\lib\site-packages\django\db\models\fields\related.py" in get_queryset
      549.                 return super(ManyRelatedManager, self).get_queryset().using(db)._next_is_sticky().filter(**self.core_filters)
    File "F:\system\env\lib\site-packages\django\db\models\query.py" in filter
      590.         return self._filter_or_exclude(False, *args, **kwargs)
    File "F:\system\env\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
      608.             clone.query.add_q(Q(*args, **kwargs))
    File "F:\system\env\lib\site-packages\django\db\models\sql\query.py" in add_q
      1198.         clause = self._add_q(where_part, used_aliases)
    File "F:\system\env\lib\site-packages\django\db\models\sql\query.py" in _add_q
      1234.                     current_negated=current_negated)
    File "F:\system\env\lib\site-packages\django\db\models\sql\query.py" in build_filter
      1100.                     allow_explicit_fk=True)
    File "F:\system\env\lib\site-packages\django\db\models\sql\query.py" in setup_joins
      1357.             names, opts, allow_many, allow_explicit_fk)
    File "F:\system\env\lib\site-packages\django\db\models\sql\query.py" in names_to_path
      1277.                                      "Choices are: %s" % (name, ", ".join(available)))
    
    Exception Type: FieldError at /url/3
    Exception Value: Cannot resolve keyword '' into field. Choices are: __app___article_user_dis_likes, __app___article_user_likes, __app___imageurl_user_dis_likes, __app___imageurl_user_likes, __app___review_user_dis_likes, __app___review_user_likes, __app___url_user_dis_likes, __app___url_user_likes, __app___videourl_user_dis_likes, __app___videourl_user_likes, article, date_joined, email, first_name, groups, id, imageurl, is_active, is_staff, is_superuser, last_login, last_name, logentry, password, review, url, user_permissions, username, userobjectpermission, videourl
    
Eleneeleni answered 11/3, 2014 at 14:56 Comment(7)
Could not reproduce on 1.6.2, URL.objects.get(pk=...).likes.all() works as expected. Is your database schema up to date with your model definition? Which Django version are you running?Thoroughfare
@Thoroughfare Yes the database schema is up to date with the model definition, i used south to add likes and dis_likes. I am using Django 1.6Eleneeleni
Can you provide the complete traceback you're getting?Thoroughfare
@Thoroughfare I have posted partial tracebackEleneeleni
Please post the full traceback. The FieldError exception you're getting does not seem to be about a URL instance, but for a User instance.Thoroughfare
@Thoroughfare The traceback originated at a template. The traceback is rather long and everything prior to the above partial traceback is related to templates and template tags. Besides, I have already stashed the changes and moved on to temp branch so that i can work on other parts of the project.Eleneeleni
show user model's meta classFulkerson
E
3

I think I have found the problem. I suppose the problem is with the name of my app which is __app__. Django field look up assumes everything before __(double underscore) is a field which in my case resolves to ``(empty string).

Always had hard time naming the default app and the project it lives in. Thought __app__ was more pythonic and clever solution. I guess I should rename my app to just app. Hope this works.

Eleneeleni answered 12/3, 2014 at 3:26 Comment(1)
__app__ is not a good idea for many reasons; instance attributes starting with __ will be mangled by the Python interpreter; documentation generally discourages use of custom __*__ identifiers.Thoroughfare
D
2

This can happen if you attempt to use __unicode__ or __str__ in places where Django expects a field name. In my case I was trying to use __unicode__, because I give my models meaningful implementations and wanted to reuse it in the first column of tables.

To get around this I added

class AdminBase(admin.ModelAdmin):

    def natural_title(self, obj):
        return unicode(obj)

    natural_title.short_description = 'Title'

where AdminBase is a custom base class for my admin classes. I can now use 'natural_title' as a field name and get the results I was seeking.

Discount answered 11/6, 2015 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.