简体   繁体   中英

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

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

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
    
    
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM