Can anyone help me with some reasonably complex Django queries?
These are my models:
class County(m开发者_JAVA百科odels.Model):
name = models.CharField(max_length=100)
class Place:
id = models.IntegerField(primary_key=True)
county = models.ForeignKey(County)
class Translation(models.Model):
place = models.OneToOneField(Place,null=True)
county = models.ForeignKey(County) # Have denormalised for ease, but could delete this.
text = models.TextField()
user = models.ForeignKey(User, null=True, blank=True)
user_ip = models.IPAddressField()
created = models.DateField(auto_now_add=True)
How can I run the following queries?
- Find the users with more than one translation, and order then by descending number of translations
- For a given county, find the places without an associated translation
- For a given county, find the 5 users with the most translations
Should I change the database setup to make this easier?
thanks!
User.objects.all().annotate(Count('translation')).order_by('-translation__count')
County.place_set.filter(translation=None)
User.objects.all().filter(
translation__county__name="my_county"
).annotate(Count("translation")).order_by('-translation__count')[:5]
精彩评论