samedi 29 novembre 2014

python - Django : impossible de résoudre le mot-clé sur la relation générique - Stack Overflow


I want to filter my generic relationship by user i.e.


Entry.objects.filter(content_object__user=request.user)

But I get this message:


Cannot resolve keyword 'content_object' into field. Choices are: content_type, id, object_id, reason, request_made


class Unsubscribe(models.Model):
"""

Notes:
See: http://www.screamingatmyscreen.com/2012/6/django-and-generic-relations/
"""
content_type = models.ForeignKey(ContentType, help_text="Represents the name of the model")
object_id = models.PositiveIntegerField(help_text="stores the object id")
content_object = generic.GenericForeignKey('content_type', 'object_id')

reason = models.CharField(max_length=60)

request_made = models.DateTimeField(auto_now_add=True,
help_text="Shows when object was created.")

UPDATE:


I found this quote:



Remember that I said there is a little difference between ForeignKey and GenericForeignKeys. You cannot use them in your queries. Entry.objects.filter(content_object...) is not possible. You can use everything else but not content_object.



So whats this is saying is there is no way I can get the x field like it was a ForeignKey?


The only solution I can see would be something like this, but cmon!


tmpl['mydict'] = []
for item in Entry.objects.all():
if request.user in item.content_object.user:
tmpl['mydict'].append(item)

then return tmpl context not the original queryset. this way tmpl only has a list of objects by that user.



I want to filter my generic relationship by user i.e.


Entry.objects.filter(content_object__user=request.user)

But I get this message:


Cannot resolve keyword 'content_object' into field. Choices are: content_type, id, object_id, reason, request_made


class Unsubscribe(models.Model):
"""

Notes:
See: http://www.screamingatmyscreen.com/2012/6/django-and-generic-relations/
"""
content_type = models.ForeignKey(ContentType, help_text="Represents the name of the model")
object_id = models.PositiveIntegerField(help_text="stores the object id")
content_object = generic.GenericForeignKey('content_type', 'object_id')

reason = models.CharField(max_length=60)

request_made = models.DateTimeField(auto_now_add=True,
help_text="Shows when object was created.")

UPDATE:


I found this quote:



Remember that I said there is a little difference between ForeignKey and GenericForeignKeys. You cannot use them in your queries. Entry.objects.filter(content_object...) is not possible. You can use everything else but not content_object.



So whats this is saying is there is no way I can get the x field like it was a ForeignKey?


The only solution I can see would be something like this, but cmon!


tmpl['mydict'] = []
for item in Entry.objects.all():
if request.user in item.content_object.user:
tmpl['mydict'].append(item)

then return tmpl context not the original queryset. this way tmpl only has a list of objects by that user.


0 commentaires:

Enregistrer un commentaire