Sunday, February 2, 2020

How to include choice value into queryset?

Dear Community Members,

How would you include choice value into queryset specified in my models.py?
When I run the following code in the terminal panel, I am getting correct results.
i = Interactions.objects.get(pk=1)
i.get_interactioncatid_display() # correct answer

My query is the following:
def get_queryset(self):
return super().get_queryset().values_list( 'company__compname', 'position__company', 'position__jobtitle', 'position__status', \
'firstname', 'lastname', 'interactions__duration', 'interactions__durationunits',\
'interactions__interactioncatid', 'position__origination').annotate(location=Concat('position__city', Value(" "), 'position__state'),\
followupdate=Max('interactions__taskdatetime'))


Instead of interactioncatid, there should be corresponding choice value.
Code for models.py is included.
from django.db import models
from django.db.models import Value, Max
from django.db.models.functions import Concat

class ContactManager(models.Manager):

def get_queryset(self):
return super().get_queryset().values_list( 'company__compname', 'position__company', 'position__jobtitle', 'position__status', \
'firstname', 'lastname', 'interactions__duration', 'interactions__durationunits',\
'interactions__interactioncatid', 'position__origination').annotate(location=Concat('position__city', Value(" "), 'position__state'),\
followupdate=Max('interactions__taskdatetime'))


class Company(models.Model):

compname = models.CharField( max_length=50, unique = True)
compwebsite = models.CharField( max_length=250, blank=True, null=True)
def __str__(self):
return self.compname
class Position(models.Model):
company = models.ForeignKey(Company, on_delete=models.CASCADE)
jobtitle = models.CharField( max_length=50)
origination = models.CharField(max_length=100, blank=True, null=True)
status = models.CharField(max_length=50, blank=True, null=True)
city = models.CharField(max_length=50, blank=True, null=True)
state = models.CharField(max_length=20, blank=True, null=True)
objects = models.Manager() # to be added to model if customer manager is being used
poscomp_objects = PosCompManager() # position and company -specific manager.
def __str__(self):
return '{} {}'.format(self.jobtitle, str(self.createdate.date()))

class Contacts(models.Model):
company = models.ForeignKey(Company, on_delete=models.CASCADE)
position = models.ForeignKey(Position, on_delete=models.CASCADE)
firstname = models.CharField( max_length=50, blank=True, null=True)
lastname = models.CharField(max_length=50, blank=True, null=True)
objects = models.Manager() # to be added to model if customer manager is being used
Contact_objects = ContactManager()

class Meta:
unique_together = [['firstname', 'lastname']]

def __str__(self):
return self.firstname + " " + self.lastname

class Interactions(models.Model):
interactionCategories = (
(1, 'Call')
,(2, 'Text')
,(3, 'Email')
,(4, 'Skype')
)
contact = models.ForeignKey(Contacts, on_delete=models.CASCADE)
interactioncatid = models.IntegerField(choices=interactionCategories, default=3) # choices
taskdatetime = models.DateTimeField( blank=True, null=True)
duration = models.IntegerField(blank=True, null=True)
durationunits = models.CharField(max_length=20, blank=True, null=True)

objects = models.Manager()
def __str__(self):
return str(self.interactioncatid)

Thank you ahead,
Leon

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACV5%2B_WdK%2BPcyVQ%2BZyfuy5JpgFQ3kpJGqhQ2pz7uExBXa_k9ow%40mail.gmail.com.

No comments:

Post a Comment