Posts

Showing posts with the label django-models

How to get the specific field of foreignkey using values function in Django?

Image
Clash Royale CLAN TAG #URR8PPP How to get the specific field of foreignkey using values function in Django? Suppose Foo, Entry are two Django models, and foo is a foreignkey in Entry. The Django QuerySet Api says that,If you have a field called foo that is a foreignkey, the default values() call will return a dictionary key called foo_id( https://docs.djangoproject.com/en/dev/ref/models/querysets). My question is how to get the specific field of foreignkey foo by values()? That is, if FIELDS is a field of fool,when I get a dictionary list of Entry, how to show FIELDS in the return dictionary, not the foo_id? 1 Answer 1 You can use double underscores to denote a field of a foreign key: Entry.objects.values('fool__FIELDS') By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and co...

Get() a Model object from it's UUID

Image
Clash Royale CLAN TAG #URR8PPP Get() a Model object from it's UUID AIM As part of a weather-dependent alarm clock project, each alarm will contain three categories of info: 1) date/time, 2) weather conditionals, 3) location. EDIT Once the User completes the SetAlarmForm , I am trying to create an Alarm object based on the Form-input and timezone , city and country based on the User's IP address. SetAlarmForm Alarm timezone city country I am attempting to retrieve the information inputted by the User in the Form , and assign that information to an Alarm object (identifiable by a UUID reference). Form Alarm As requested in a comment, the URL is https://jqlc3gdp.apps.lair.io/login/ CODE Models.py class User(models.Model): """ Model representing each User """ username = models.CharField(max_length=10, unique=True) class Alarm(models.Model): """ Model representing each Alarm """ alarm_id = models.UUID...

Django - detect which form was submitted in a page

Image
Clash Royale CLAN TAG #URR8PPP Django - detect which form was submitted in a page I have multiple form fields, and multiple submit buttons on a same page. I want to detect by which form field a POST request is triggered. I am using CBV and my post() method has this: CBV post() def post(self, request, **kwargs): .... form1 = Form1(request.POST, instance=Model1.objects.filter(some_filtering...) form2 = Form2(request.POST, instance=Model2.objects.filter(some_filtering...) form3 = Form3(request.POST, instance=Model3.objects.filter(some_filtering...) form4 = Form4(request.POST, instance=Model4.objects.filter(some_filtering...) # this is the code I want to know if POST request is triggered by form1... # do something.... return super().post(request, **kwargs) return super().post(request, **kwargs) How can I detect which form triggered POST request? 2 Answers 2 ...

Django: prefetch_related has no effect

Django: prefetch_related has no effect I'm trying to optimize a DB query using prefetch_related without success. prefetch_related class Order(models.Model): # some fields ... @property def last_operation(self) -> Optional['OrderOperation']: try: return self.orderoperation_set.latest() except OrderOperation.DoesNotExist: return None @property def total(self) -> Optional[Decimal]: last_operation = self.last_operation return last_operation.total if last_operation else None class OrderOperation(TimeStampable, models.Model): order = models.ForeignKey(Order) total = DecimalField(max_digits=9, decimal_places=2) orders = Order.objects.prefetch_related('orderoperation_set') # There are 1000 orders result = sum([order.total for order in orders]) len(connection.queries) >>> 1003 As we can see, there is one query per order.total , so 1000 queries, that makes the whole request ve...