Hi all. There was a need for group_by (as I am inclined to assume),
and all else fails.

Models:
class States (models.Model):
    state = models.CharField (max_length = 70)

class Person (models.Model):
    name = models.CharField (max_length = 100)
    login = models.CharField (max_length = 30)

class Hosts (models.Model):
    host = models.CharField (max_length = 30)
    ip = models.IPAddressField ()

class Home (models.Model):
    person = models.ForeignKey (Person)
    state = models.ForeignKey (States)
    date = models.DateTimeField ()
    host = models.ForeignKey (Hosts)
    time_spent = models.PositiveIntegerField (null = True)

class Online (models.Model):
    date = models.DateTimeField ()
    person = models.ForeignKey (Person)
    host = models.ForeignKey (Hosts)

Objective: To select all of the Online users and their past record of
the Home (in particular, the field state). In other words: from Home
to choose the last entry of the user that is in Online.

mysql-query will look something like this:
SELECT
MAX (`main_home`. `Date`), p.login, st.state
FROM `main_home`, main_states st, main_person p
WHERE
  p.id = main_home.person_id
and
  st.id = `main_home`. state_id
and
  `Main_home`. `Person_id`
    IN
        (SELECT U0. `Person_id` FROM `main_online` U0)
GROUP BY `main_home`. `Person_id`, `main_home`. `Person_id`
ORDER BY `main_home`. `Date` DESC

Leave any sensible expression for the task does not work well in any
way. Here are the options:
ok = Home.objects.order_by ('-date'). filter (person__in =
Online.objects.all (). values ​​('person__pk'))
ok.query.group_by = ['person_id']
(Makes not the recent records)

ok = Home.objects.values ​​('date'). filter (person__in =
Online.objects.all (). values ​​('person__pk')). annotate (Max
('date')). order_by ('-date')
ok.query.group_by = ['person_id']
(Also gives not just the expected result)

Found an alternative solution: denormalize database (adding to the
model, for example, Person, fields with necessary data)

I can also use the direct sql query in the code. But on cellular it
level does not seem right -)

Tables innodb.

I'd be glad of any help.

If I forgot something - ask.
Django study has just started, according to this request chide
severely in order to consolidate the material.

Thank you for your attention.

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