20👍
You can reference foreign key attributes in values function:
MyEvent.objects.all().values('msg','event_type', 'msg__name', 'msg__description')
👤nima
4👍
You can create a list of field names with msg__
prepended to each value using list comprehension based on MyMessage._meta
. Then simply unpack the values using .values(*list_of_fields)
.
Other than that, it is not possible. values()
only accepts field names as positional arguments, so you’ll have to implicitly generate the arguments before calling values()
.
👤knbk
- Can we have Django DateTimeField without timezone?
- Django REST-Auth Password Reset
- Reset SQLite database in Django
- Django – 'WhereNode' object has no attribute 'output_field' error
- Django Admin + Filter Horizontal?
0👍
You should add related name to foreign key like this
class MyEvent(models.Model):
msg = models.ForeignKey(MyMessage,related_name='message')
event_type = models.IntegerField(choices=EVENTS_TYPES)
class MyMessage(models.Model):
notification = models.IntegerField(choices=EVENTS_TYPES2)
name = models.CharField(max_length=20, null=False, blank=False)
related_name=description = models.CharField(max_length=150, null=False, blank=False)
Then you can retrieve it with related name. like this:
objs=MyEvent.objects.all()
objs[0].message.all() # this is the Mymessage record
then you can create your list whatever you like
- Django filter datetime base on date only
- Making a text input field look disabled, but act readonly
- Django on Heroku – Broken Admin Static Files
- IPython doesn't work in Django shell
Source:stackexchange.com