Home > other >  need id's of objects from Django query where customer id's are distinct - DB mysql
need id's of objects from Django query where customer id's are distinct - DB mysql

Time:10-12

Models -

class CfgXmppSessions(models.Model):

     objid = models.AutoField(primary_key=True)

     customer_objid = models.ForeignKey('CstContact', blank=True, null=True, on_delete=models.DO_NOTHING,db_constraint=False, db_column='customer_objid', db_index=False)

     character_name = models.CharField(max_length=45)
     character_channel_type = models.CharField(max_length=45, blank=True, null=True)

     character_objid = models.ForeignKey('PerfCharacter', blank=True, null=True, on_delete=models.DO_NOTHING,db_constraint=False, db_column='character_objid', db_index=False)

I want all objid where customers are unique

CodePudding user response:

try something like:

def GetObjectID():
    customerIDs = list(CfgXmppSessions.objects.values('customer_objid').distinct())
    objectIDs = []
    for id in customerIDs:
        objectIDs.append(CfgXmppSessions.objects.filter(customer_objid = f"{id}").values("customer_objid"))
    return objectIds
    
    

CodePudding user response:

objids = [session.get('max_objid') for session in list(CfgXmppSessions.objects.filter(character_objid=9100).values('customer_objid').annotate(max_objid=Max('objid')))]

This works for me.

  • Related