#29727: Lookup using F() on a non-existing model field doesn't throw an error -------------------------------------+------------------------------------- Reporter: | Owner: nobody vidyarani-dg | Type: Bug | Status: new Component: Database | Version: 2.1 layer (models, ORM) | Severity: Normal | Keywords: F() expressions Triage Stage: | Has patch: 0 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- Fetching the value of a non-existing field on a model using F() object does not throw an error.
Instead, it is just giving the FK value of the instance. Please find below a minimal test case to reproduce this issue. {{{ #!python # lineitem/models.py from django.db import models class Account(models.Model): REGULAR = 'R' GOLD = 'G' PLATINUM = 'P' ACCOUNT_TYPE_CHOICES = ( (REGULAR, 'Regular'), (GOLD, 'Gold'), (PLATINUM, 'Platinum'), ) account_type = models.CharField( max_length=1, choices=ACCOUNT_TYPE_CHOICES, default=REGULAR, ) class Client(models.Model): name = models.CharField(max_length=50) registered_on = models.DateField() account_type = models.ForeignKey(Account, on_delete=models.CASCADE) $ python manage.py shell In [1]: from datetime import date, timedelta In [2]: from models import Client, Account In [3]: Client.objects.create( ...: name='Jane Doe', ...: account_type=Account.objects.create(account_type=Account.REGULAR), ...: registered_on=date.today() - timedelta(days=36) ...: ) ...: Client.objects.create( ...: name='James Smith', ...: account_type=Account.objects.create(account_type=Account.GOLD), ...: registered_on=date.today() - timedelta(days=5) ...: ) ...: Client.objects.create( ...: name='Jack Black', ...: account_type=Account.objects.create(account_type=Account.PLATINUM), ...: registered_on=date.today() - timedelta(days=10 * 365) ...: ) ...: ...: Out[3]: <Client: Client object (6)> In [4]: from django.db.models import F In [5]: Client.objects.values(client_name=F('name'), account_name=F('account_type__name')) Out[5]: <QuerySet [{'client_name': 'Jane Doe', 'account_name': 2}, {'client_name': 'James Smith', 'account_name': 3}, {'client_name': 'Jack Black', 'account_name': 4}, {'client_name': 'Jane Doe', 'account_name': 5}, {'client_name': 'James Smith', 'account_name': 6}, {'client_name': 'Jack Black', 'account_name': 7}]> In [6]: print(queryset.query) SELECT "lineitem_client"."name" AS "client_name", "lineitem_client"."account_type_id" AS "account_name" FROM "lineitem_client" }}} -- Ticket URL: <https://code.djangoproject.com/ticket/29727> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/055.417bbf81d2ddb3305491a76f034d2ad1%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.