On 05-12-11 23:10, wgis wrote:
Hi guys,
I'm trying to solve this without raw SQL, from the past 4/5 days.
I've been also researching a lot to understand more complex queries in
django so I would realy appreciate your help since I plan to continue
using django in my projects.



I have a:

class VoteContext(models.Model):

        name = models.CharField(max_length = 15, unique = True)

class Vote(models.Model):

        thing = models.ForeignKey(Thing, null = False)
        context = models.ForeignKey(VoteContext, null = False)
        user = models.ForeignKey(User, null = False)
        vote = models.DecimalField(default = 0, max_digits = 3,
decimal_places = 1, null = False)

         class Meta:
                unique_together = ("thing", "user", "context")


Something like this in the database: (I will put names instead of IDs
for easier understanding)

mydatabase_votecontext
(id, name)
(1, Flavour)
(2, Smell)
(3, Usability)
(4, Size)

mydatabase_vote
(id, thing, context, user, vote)

(1, Potatoes, Flavour, Me, 2.0)
(2, Potatoes, Smell, Me, 4.3)
(3, Potatoes, Usability, Me, 4.0)
(4, Carrots, Flavor, Me, 3.0)
(5, Cars, Smell, Me, 4.2)
(6, Cars, Usability, Me, 4.9)


I would like to make a query for ->  one specific "thing"<-, like,
Carrots, being the result:

(Carrots, Flavour, 2.0)
(Carrots, Smell, 0.0)
(Carrots, Usability, 0.0)
(Carrots, Size, 0.0)

or

(Carrots, Flavour, 2.0)
(Carrots, Smell, null)
(Carrots, Usability, null)
(Carrots, Size, null)

So what you want to get back is votes, right?

Vote.objects.all()


But... filtered for one specific thing:

Vote.objects.filter(thing=carrot)  # Assuming carrot is known.
Vote.objects.filter(thing__id=4)  # Assuming you want it by ID.


And then you only want three specific values:

Vote.objects.filter(thing=carrot).values_list('thing', 'context', 'vote)

See
https://docs.djangoproject.com/en/1.3/ref/models/querysets/#values-list
for how that last trick works.


Something like that?


Reinout

--
Reinout van Rees                    http://reinout.vanrees.org/
rein...@vanrees.org             http://www.nelen-schuurmans.nl/
"If you're not sure what to do, make something. -- Paul Graham"

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

Reply via email to