I have these two rather simple models that looks something like this:

class MyModel(models.Model):
      myName=models.CharField(max_length=60)

class MyOtherModel(models.Model):
       myString=models.CharField(max_length=60)
       myModel=models.ForeignKey(MyModel)

I will eventually have quite a number of MyOtherModelinstances. I have
made a form where the users enters a string and a myModel instance,
and it gets saved to the database. I need a query to get all the
MyModel instances that has a certain stringvalue in myOtherModel, but
only if it is the last entered one. My current solution is this:

for i in mymodel.myothermodel_set.all():
 
myOtherModel=MyOtherModel.objects.filter(myModel=i).order_by('-id')[:
1].get()
         if myOtherModel.myString==certainStringValue:
               objectsToRetur.append(i)

This works, but I think it's not a very good. Is there maybe a better
solution?

Thanks!

Odd-R.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to