Django - Check if newer value exist for each item in queryset -


i have model hold users reactions , have few types of reactions - 1,2,3,4. user can react few times different values , want keep them in db. model (i'm using generic foreign key, not sure if matters).

class reaction(models.model):     user = models.foreignkey('auth.user', on_delete=models.set_null, null=true)     date = models.datetimefield("reaction date", auto_now_add=true)     content_type = models.foreignkey(contenttype, verbose_name=_('content type'),                                      related_name="content_type_for_%(class)s", on_delete=models.cascade)      object_pk = models.textfield(_('object id'))     root_obj = genericforeignkey(ct_field="content_type", fk_field="object_pk")     react = models.charfield('reaction', max_length=10) 

i want the root_obj (distinct if possible) user reacted specific reaction (e.g. 2), not reaction of type 3,4 afterwards.

is possible? thanks

r

i hope did correctly. see if works:

def view(request, object_pk): query = reaction.objects.filter(user=request.user, object_pk=object_pk) \                         .only('react', 'date', 'root_obj')  try:                            react2 = query.get(react='2') except:     react2 = none  try:                            react3 = query.get(react='3') except:     react3 = none  try:                            react4 = query.get(react='4') except:     react4 = none  # there reaction = 2 , not reaction = 3 or 4? if react2 , not react3 or not react4:     return react2.root_obj  # there reaction = 3 or 4 along 2? elif react2 , react3 or react4:      if react3:          if react2.date < react3.date:             return react2.root_obj         return none     elif react4:          if react2.date < react4.date::             return react2.root_obj         return none return none 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -