Wednesday, February 28, 2018

Re: Window expression inside Subquery

It seems to be a bug in Django. Here's the ticket (https://code.djangoproject.com/ticket/29172) with
patch and tests if anyone is interested in this topic.

Cheers,
   Tom


Dne čtvrtek 1. března 2018 7:38:13 UTC+1 Tomáš Ehrlich napsal(a):
Hey folks,
I'm getting an AttributeError: 'NoneType' object has no attribute 'relabeled_clone'
when using Window expression inside Subquery:

Message.objects
.filter(pk__in=Subquery(
    Message.objects
    .annotate(latest_pk=Window(
        expression=FirstValue('pk'),
        partition_by=[F('conversation_id')],
        order_by=F('date').desc(),
    ))
    .values('latest_pk')
))

I would like to translate this SQL statement to Django ORM:

SELECT
  "conversations_message"."id",
  "conversations_message"."conversation_id",
  "conversations_message"."author_id",
  "conversations_message"."content",
  "conversations_message"."date"
FROM "conversations_message"
WHERE "conversations_message"."id" IN (
  SELECT first_value("id") OVER (PARTITION BY "conversation_id" ORDER BY "date" DESC)
  FROM conversations_message
)

I tested SQL statement and it works. I'm trying to select all conversations in DB
and prefetch latest message in each conversation.


I've found this note about using aggregates in Subqueries:
but it doesn't seem to be related to my case.

I could however replace Window function with Max aggregate:

Message.objects.filter(pk__in=Subquery(
    Message.objects
        .order_by()
        .values('conversation')
        .annotate(latest=Max('id'))
        .values('latest')
    )
)

This works too, but I don't feel very comfortable using Max on `id`.


Related question: Is there a better way to prefetch latest related items? Both in Django and raw SQL.

Thanks in advance!


Cheers,
   Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9558fa54-089d-4dd3-8e60-09fdf63712ec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Fetching next and/or prior objects given an arbitrary ordering

Mike,

Yep, adding pk as a final tie breaker is trivial, but not the issue ;-). Alas adding a sequencing field is not an option because I am looking for a generic solution, akin to what the admin site on Django offers, I have a model browser that I want to browse any of my models with.  And I was in fact using a date_time sequencer, only found that it broke in the generic sense for any ordering and with ties.

Moreover, without understanding the full context in your example you seem be fetching a pile of questions and then walking them to find the prior (I prefer the term "prior" as "last" has unfortunate ambiguity) and next questions, your set defined by "instruction_id=question.instruction.pk". My guess is that in your scenario that list is likely to be small (<1000 objects ;-) and this strategy becomes unappealing again if that list could be arbitrarily large (millions, or more) which is true for a generic scenario which makes no assumptions about the model other than what is provided by its ordering and known about the current object.

Regards,

Bernd.

Mike Dewhirst wrote:
On 1/03/2018 10:50 AM, Bernd Wechner wrote:  > Julio,  >  > Thanks for giving it some though. But I think you misread me a little.   > I am using the get() only to illustrate that the precondition is, I   > have a single object. The goal then is find a neighboring object (as   > defined by the ordering in the model).  >  > Yes indeed, a filter is the first and primary candidate for achieving   > that, but can you write one that respects an abritrary ordering   > involving multiple fields, as I exemplified with:  >  > |  > classThing(models.Model):  >      field1 =...  >      field2 =...  >      field2 =...  > classMeta:  >          ordering =['field1','-field2','field3']  > |  >  > Also consider that the ordering thus specified does not stipulate   > uniqueness in any way, that is many neighboring things in an ordered   > list may have identical values of field1, field2 and field3.    Nothing to stop you adding 'pk' or '-pk' to the ordering list.    I have done something similar recently where an online training course   leads the user with next and previous links to the next or previous   questions. At the first and last question for each set of instruction,   the links morph into previous and next instruction.    My approach relies on there being only a few questions per instruction.   It isn't very elegant because it tramples over the 'last' values until   the last man standing is correct and it bails out as soon as it finds a   greater 'next' value.    I did need/want a separate numeric ordering field because a trainer   writing up a course needs a sequence to force instruction items and   questions into adjustable arbitrary orders. Hence, the model save()   method updates a num_sequence float field from the user entered sequence   char field.    I haven't thought about < or > in the context of your ['field1',   '-field2', 'field3'] ordering but I went for a num_sequence float field   because '11' < '2' but 2.0 < 11.0 and so on.    The view code goes like this ...    def get_last_next_question_links(question, instruction, course, user):       lastlink = 'No previous question'       nextlink = 'No more questions'       questions = Question.objects.filter(instruction_id=question.instruction.pk)       for obj in questions:           if obj.num_sequence < question.num_sequence:               lasturl = '/question/%s/' % obj.pk               lastname = 'Previous Question %s' % obj.sequence               lastlink = '<a href="%s">%s</a>' % (lasturl, lastname)           elif obj.num_sequence > question.num_sequence:               nexturl = '/question/%s/' % obj.pk               nextname = 'Next Question %s' % obj.sequence               nextlink = '<a href="%s">%s</a>' % (nexturl, nextname)               break       if lastlink.startswith('No'):           lastlink = get_instruction_link(               course, instruction, user, next=False           )       if nextlink.startswith('No'):           nextlink = get_instruction_link(               course, instruction, user, next=True           )       return lastlink, nextlink      hth    >  > I'm not sure how Django sorts those ties, but imagine it either defers   > to the underlying database engine (i.e. uses the sort simply to   > generate an ORDER BY clause in the SQL for example in the above case:  >  > |  > ORDER BY field1 ASC,field2 DESC,field3 ASC  > |  >  > and lets the underlying database engine define how ties are ordered.   > Or it could add a pk tie breaker to the end. Matters little, the   > problem remains: how to find neighbors given an arbitrary ordering and   > ties.  >  > Can you write a filter clause to do that? I'm curious on that front.  >  > It's easy of course with one sort field with unique values collapsing   > to an __gt or __lt filter folllowed by first() or last() respectively   > (not sure that injects a LIMIT clause into the SQL or collects a list   > and then creams one element from it - I'll test a little I think).  >  > In the mean time, I still feel this has to be a fairly standard use   > case. It's about browsing objects in a table one by one, with a next   > and previous button given an ordering specified in the model and no   > guarantee of uniqueness on the (sort keys).  >  > Regards,  >  > Bernd.  >  > On Thursday, 1 March 2018 00:58:58 UTC+11, Julio Biason wrote:  >  >     Hi Bernd,  >  >     Well, the thing with `get()` is that it will return only one  >     object. What you're looking for is `filter()`.  >  >     Say, you want all the things that have an ID after a certain  >     value. So you get `list_of_things =  >     Things.objects.filter(pk__gte=...)`. Now it'll return the list,  >     with all elements after the one you asked.  >  >     If you want the previous and next, you can do `list_of_previous =  >     Things.objects.filter(pk__lt=...).limit(1)` and `list_of_next =  >     Things.objects.filter(pk__gt).limit(1)`.  >  >     Or something like that ;P  >  >     On Wed, Feb 28, 2018 at 8:56 AM, Bernd Wechner  >     <bernd....@gmail.com <javascript:>> wrote:  >  >         I'm a bit stumped on this. Given an arbitrary ordering as  >         specified by the ordering meta option:  >  >         https://docs.djangoproject.com/en/2.0/ref/models/options/#ordering  >         <https://docs.djangoproject.com/en/2.0/ref/models/options/#ordering>  >  >         for example:  >  >             class Thing(models.Model):  >                 field1 = ...  >                 field2 = ...  >                 field2 = ...  >                 class Meta:  >                     ordering = ['field1', '-field2', 'field3']  >  >         given an instant of Thing:  >  >             thing = Thing.objects.get(pk=...)  >  >         how can I get the next Thing after that one, and/or the prior  >         Thing before that one as they appear on the sorted list of Things.  >  >         It's got me stumped as I can't think of an easy way to build a  >         filter even with Q object for an arbitrary ordering given  >         there can be multiple fields in ordering and multiple Things  >         can have the same ordering list (i.e. there can be ties - that  >         Django must resolve either arbitrarily or with an implicit pk  >         tie breaker on ordering).  >  >         It's got me stumped. I can solve any number of simpler  >         problems just not his generic one (yet).  >  >         Ideally I'd not build a list of all objects (waste of memory  >         with large collections), and look for my thing in the list and  >         then pick out the next or prior.  >  >         I'd ideally like to fetch it in one query returning the one  >         Thing, or if not possible no worse than returning all Things  >         on side of it and picking off the first or last respectively  >         (even that's kludgy IMHO).  >  >         I'm using postgresql and I found a related question here:  >  >         https://dba.stackexchange.com/questions/53862/select-next-and-previous-rows  >         <https://dba.stackexchange.com/questions/53862/select-next-and-previous-rows>  >  >         but would rather stick with the ORM and not even explore SQL  >         (just took a peak to see SQL can be constructed to do it I  >         guess, as if not, the ORM sure won't have a good way of doing  >         it methinks).  >  >         I'd have thought this a sufficiently common use case but am  >         perhaps wrong there, with most sites exploiting simple  >         orderings (like date_time or creation say). But I want to  >         build a generic solution that works on any model I write, so I  >         can walk through the objects in the order specified by  >         ordering, without building a list of all of them. In short I  >         want to solve this problem, not reframe the problem or work  >         around it ;-).  >  >         Regards,  >  >         Bernd.  >  >         --   >         You received this message because you are subscribed to the  >         Google Groups "Django users" group.  >         To unsubscribe from this group and stop receiving emails from  >         it, send an email to django-users...@googlegroups.com  >         <javascript:>.  >         To post to this group, send email to  >         django...@googlegroups.com <javascript:>.  >         Visit this group at  >         https://groups.google.com/group/django-users  >         <https://groups.google.com/group/django-users>.  >         To view this discussion on the web visit  >         https://groups.google.com/d/msgid/django-users/751c367c-d5e9-e06b-8f5c-82054f11a9ab%40gmail.com  >         <https://groups.google.com/d/msgid/django-users/751c367c-d5e9-e06b-8f5c-82054f11a9ab%40gmail.com?utm_medium=email&utm_source=footer>.  >         For more options, visit https://groups.google.com/d/optout  >         <https://groups.google.com/d/optout>.  >  >  >  >  >     --   >     *Julio Biason*,Sofware Engineer  >     *AZION*  | Deliver. Accelerate. Protect.  >     Office: +55 51 3083 8101 |  Mobile: +55 51 _99907 0554_  >  > --   > You received this message because you are subscribed to the Google   > Groups "Django users" group.  > To unsubscribe from this group and stop receiving emails from it, send   > an email to django-users+unsubscribe@googlegroups.com   > <mailto:django-users+unsubscribe@googlegroups.com>.  > To post to this group, send email to django-users@googlegroups.com   > <mailto:django-users@googlegroups.com>.  > Visit this group at https://groups.google.com/group/django-users.  > To view this discussion on the web visit   > https://groups.google.com/d/msgid/django-users/bf6698d0-9aa5-4251-be69-62fe53afb603%40googlegroups.com   > <https://groups.google.com/d/msgid/django-users/bf6698d0-9aa5-4251-be69-62fe53afb603%40googlegroups.com?utm_medium=email&utm_source=footer>.  > For more options, visit https://groups.google.com/d/optout.    


Window expression inside Subquery

-----BEGIN PGP SIGNATURE-----

iQIzBAEBCgAdFiEEgp5wx8+ggeLmQKiikpDftiGHlegFAlqXn1QACgkQkpDftiGH
lejjJA//cJexc9FXOkehvJplkUStDzazus/k2bqu07MzTF4xzLsKQOKXccTtnZH8
GRehFK2G5xKFPWoaOneAm1LtK0ycxTBsSd3imnB/VF9Vvv13c1P4PZ9NTVZ1GTiA
czBiP4VnUnbGUVraoHGWjPZMNrhcZfL9+eeoKO8tblQw3Hve9W2gnmEwvT7IRzlA
axGm8cJIu0nJsEyLZqgFvboC9SPAx2EqFAfvOLc3eGxQbXxcDuwzHQQ3YGv5Mjy1
PzffUszneNBQcxDyCxqXp1h6pXdbgmCbJC/KK+/SAsSFnXfvpORxdP7/byx+4tvL
rd84yV3ZpRaTSJ8pBKcYv0WviWFQHaaDmWsYDXAkBmsR5/SQHnqtrh/3GR8wKMDI
f7hyp/0kUhlz5OPhV89jJ/pLmDycivH70+wehPl6S4s/un9M4cS2V2GQ3qnT9IY/
oTsiAg1vomDzdUJdDOHmPxxhTz3jUcHEXs+HFoi6UiDoe9x7iyr1AaLhRHAp9sWF
Ntkh5t6f2dqP6ZbkOqsIprGCUHK0i0NqsbdlXQoaxrLi+kW7i9ohZXI1pNEgjnFM
9u5QY7d4NRWyxms0kQeV2P4y4wv+Wx+0FQdOxC/Ar46qkperkC0WytyVBCSHj0W2
jFYkdUv/FzTEsTuLLbWGtlOwIYHzykW1ftbij8nWPKdSumrN+aE=
=Uvn0
-----END PGP SIGNATURE-----
Hey folks,
I'm getting an AttributeError: 'NoneType' object has no attribute 'relabeled_clone'
when using Window expression inside Subquery:

Message.objects
.filter(pk__in=Subquery(
    Message.objects
    .annotate(latest_pk=Window(
        expression=FirstValue('pk'),
        partition_by=[F('conversation_id')],
        order_by=F('date').desc(),
    ))
    .values('latest_pk')
))

I would like to translate this SQL statement to Django ORM:

SELECT
  "conversations_message"."id",
  "conversations_message"."conversation_id",
  "conversations_message"."author_id",
  "conversations_message"."content",
  "conversations_message"."date"
FROM "conversations_message"
WHERE "conversations_message"."id" IN (
  SELECT first_value("id") OVER (PARTITION BY "conversation_id" ORDER BY "date" DESC)
  FROM conversations_message
)

I tested SQL statement and it works. I'm trying to select all conversations in DB
and prefetch latest message in each conversation.


I've found this note about using aggregates in Subqueries:
but it doesn't seem to be related to my case.

I could however replace Window function with Max aggregate:

Message.objects.filter(pk__in=Subquery(
    Message.objects
        .order_by()
        .values('conversation')
        .annotate(latest=Max('id'))
        .values('latest')
    )
)

This works too, but I don't feel very comfortable using Max on `id`.


Related question: Is there a better way to prefetch latest related items? Both in Django and raw SQL.

Thanks in advance!


Cheers,
   Tom

Re: Fetching next and/or prior objects given an arbitrary ordering

On 1/03/2018 10:50 AM, Bernd Wechner wrote:
> Julio,
>
> Thanks for giving it some though. But I think you misread me a little.
> I am using the get() only to illustrate that the precondition is, I
> have a single object. The goal then is find a neighboring object (as
> defined by the ordering in the model).
>
> Yes indeed, a filter is the first and primary candidate for achieving
> that, but can you write one that respects an abritrary ordering
> involving multiple fields, as I exemplified with:
>
> |
> classThing(models.Model):
>      field1 =...
>      field2 =...
>      field2 =...
> classMeta:
>          ordering =['field1','-field2','field3']
> |
>
> Also consider that the ordering thus specified does not stipulate
> uniqueness in any way, that is many neighboring things in an ordered
> list may have identical values of field1, field2 and field3.

Nothing to stop you adding 'pk' or '-pk' to the ordering list.

I have done something similar recently where an online training course
leads the user with next and previous links to the next or previous
questions. At the first and last question for each set of instruction,
the links morph into previous and next instruction.

My approach relies on there being only a few questions per instruction.
It isn't very elegant because it tramples over the 'last' values until
the last man standing is correct and it bails out as soon as it finds a
greater 'next' value.

I did need/want a separate numeric ordering field because a trainer
writing up a course needs a sequence to force instruction items and
questions into adjustable arbitrary orders. Hence, the model save()
method updates a num_sequence float field from the user entered sequence
char field.

I haven't thought about < or > in the context of your ['field1',
'-field2', 'field3'] ordering but I went for a num_sequence float field
because '11' < '2' but 2.0 < 11.0 and so on.

The view code goes like this ...

def get_last_next_question_links(question, instruction, course, user):
lastlink = 'No previous question'
nextlink = 'No more questions'
questions = Question.objects.filter(instruction_id=question.instruction.pk)
for obj in questions:
if obj.num_sequence < question.num_sequence:
lasturl = '/question/%s/' % obj.pk
lastname = 'Previous Question %s' % obj.sequence
lastlink = '<a href="%s">%s</a>' % (lasturl, lastname)
elif obj.num_sequence > question.num_sequence:
nexturl = '/question/%s/' % obj.pk
nextname = 'Next Question %s' % obj.sequence
nextlink = '<a href="%s">%s</a>' % (nexturl, nextname)
break
if lastlink.startswith('No'):
lastlink = get_instruction_link(
course, instruction, user, next=False
)
if nextlink.startswith('No'):
nextlink = get_instruction_link(
course, instruction, user, next=True
)
return lastlink, nextlink


hth

>
> I'm not sure how Django sorts those ties, but imagine it either defers
> to the underlying database engine (i.e. uses the sort simply to
> generate an ORDER BY clause in the SQL for example in the above case:
>
> |
> ORDER BY field1 ASC,field2 DESC,field3 ASC
> |
>
> and lets the underlying database engine define how ties are ordered.
> Or it could add a pk tie breaker to the end. Matters little, the
> problem remains: how to find neighbors given an arbitrary ordering and
> ties.
>
> Can you write a filter clause to do that? I'm curious on that front.
>
> It's easy of course with one sort field with unique values collapsing
> to an __gt or __lt filter folllowed by first() or last() respectively
> (not sure that injects a LIMIT clause into the SQL or collects a list
> and then creams one element from it - I'll test a little I think).
>
> In the mean time, I still feel this has to be a fairly standard use
> case. It's about browsing objects in a table one by one, with a next
> and previous button given an ordering specified in the model and no
> guarantee of uniqueness on the (sort keys).
>
> Regards,
>
> Bernd.
>
> On Thursday, 1 March 2018 00:58:58 UTC+11, Julio Biason wrote:
>
> Hi Bernd,
>
> Well, the thing with `get()` is that it will return only one
> object. What you're looking for is `filter()`.
>
> Say, you want all the things that have an ID after a certain
> value. So you get `list_of_things =
> Things.objects.filter(pk__gte=...)`. Now it'll return the list,
> with all elements after the one you asked.
>
> If you want the previous and next, you can do `list_of_previous =
> Things.objects.filter(pk__lt=...).limit(1)` and `list_of_next =
> Things.objects.filter(pk__gt).limit(1)`.
>
> Or something like that ;P
>
> On Wed, Feb 28, 2018 at 8:56 AM, Bernd Wechner
> <bernd....@gmail.com <javascript:>> wrote:
>
> I'm a bit stumped on this. Given an arbitrary ordering as
> specified by the ordering meta option:
>
> https://docs.djangoproject.com/en/2.0/ref/models/options/#ordering
> <https://docs.djangoproject.com/en/2.0/ref/models/options/#ordering>
>
> for example:
>
> class Thing(models.Model):
>     field1 = ...
>     field2 = ...
>     field2 = ...
>     class Meta:
>         ordering = ['field1', '-field2', 'field3']
>
> given an instant of Thing:
>
> thing = Thing.objects.get(pk=...)
>
> how can I get the next Thing after that one, and/or the prior
> Thing before that one as they appear on the sorted list of Things.
>
> It's got me stumped as I can't think of an easy way to build a
> filter even with Q object for an arbitrary ordering given
> there can be multiple fields in ordering and multiple Things
> can have the same ordering list (i.e. there can be ties - that
> Django must resolve either arbitrarily or with an implicit pk
> tie breaker on ordering).
>
> It's got me stumped. I can solve any number of simpler
> problems just not his generic one (yet).
>
> Ideally I'd not build a list of all objects (waste of memory
> with large collections), and look for my thing in the list and
> then pick out the next or prior.
>
> I'd ideally like to fetch it in one query returning the one
> Thing, or if not possible no worse than returning all Things
> on side of it and picking off the first or last respectively
> (even that's kludgy IMHO).
>
> I'm using postgresql and I found a related question here:
>
> https://dba.stackexchange.com/questions/53862/select-next-and-previous-rows
> <https://dba.stackexchange.com/questions/53862/select-next-and-previous-rows>
>
> but would rather stick with the ORM and not even explore SQL
> (just took a peak to see SQL can be constructed to do it I
> guess, as if not, the ORM sure won't have a good way of doing
> it methinks).
>
> I'd have thought this a sufficiently common use case but am
> perhaps wrong there, with most sites exploiting simple
> orderings (like date_time or creation say). But I want to
> build a generic solution that works on any model I write, so I
> can walk through the objects in the order specified by
> ordering, without building a list of all of them. In short I
> want to solve this problem, not reframe the problem or work
> around it ;-).
>
> Regards,
>
> Bernd.
>
> --
> You received this message because you are subscribed to the
> Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from
> it, send an email to django-users...@googlegroups.com
> <javascript:>.
> To post to this group, send email to
> django...@googlegroups.com <javascript:>.
> Visit this group at
> https://groups.google.com/group/django-users
> <https://groups.google.com/group/django-users>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/751c367c-d5e9-e06b-8f5c-82054f11a9ab%40gmail.com
> <https://groups.google.com/d/msgid/django-users/751c367c-d5e9-e06b-8f5c-82054f11a9ab%40gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
>
>
> --
> *Julio Biason*,Sofware Engineer
> *AZION*  | Deliver. Accelerate. Protect.
> Office: +55 51 3083 8101 |  Mobile: +55 51 _99907 0554_
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto:django-users@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/bf6698d0-9aa5-4251-be69-62fe53afb603%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/bf6698d0-9aa5-4251-be69-62fe53afb603%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a0fdbce4-bdc4-5c84-d2c3-d797f2ad0972%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

Re: Is there a way to make a field both foreignKey or allow user to fill it in themself?

On 1/03/2018 3:20 PM, Matemática A3K wrote:
>
>
> On Wed, Feb 28, 2018 at 12:46 AM, Mike Dewhirst <miked@dewhirst.com.au
> <mailto:miked@dewhirst.com.au>> wrote:
>
> On 28/02/2018 1:58 PM, Alexander Joseph wrote:
>
> Sorry, I think my question was confusing. What I want to do is
> allow the user to either select a user from a drop down list
> of users from the ForeignKey in the Users model, or if there
> is not a user suitable to select in the drop down, they can
> manually type in a name themselves.
>
>
> In the same field that would require a hack of the first water. 
> Way above my pay grade. For me, anything which equates an existing
> user object with a string typed in is likely to end in misery.
>
> Otherwise a separate CharField for the typed in name and null=True
> and blank=True in the ForeignKey ought to work.
>
> Mike
>
>
> Indeed, what you can also do - besides the nullable FK, is process
> that in the Form where if the FK is null and the the charfield is not
> None, then create a User (maybe non active) with that name and assign
> it to the FK.

It will depend on the use-case requirements but I think that could get
slightly messy. You need to ensure you are not creating duplicate
people. For example you would likely need case-insensitive names.



>
>
> Thanks for your reply
>
>
>
> On Tuesday, February 27, 2018 at 7:45:51 PM UTC-7, Mike
> Dewhirst wrote:
>
>     On 28/02/2018 1:26 AM, Alexander Joseph wrote:
>     > Is there a way to make a form field or model field
> either a foreign
>     > key reference or allow the user to fill in something
> themselves?
>     I'm
>     > making a fixed assets app and each fixed asset has an
> owner field,
>     > which I would like to assign to an app user if possible,
> and if not
>     > possible be able to fill in a name myself.
>
>     Yes. Make the ForeignKey in the model null=True and
> blank=True so
>     it can
>     exist all alone. Then you can limit_choices_to whatever
> whatever
>     suits
>     your requirements ...
>
> https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to
> <https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to>
>    
> <https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to
> <https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to>>
>
>
>     hth
>     Mike
>
>
>     >
>     > Thanks
>     > --
>     > You received this message because you are subscribed to
> the Google
>     > Groups "Django users" group.
>     > To unsubscribe from this group and stop receiving emails
> from
>     it, send
>     > an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com> <javascript:>
>     > <mailto:django-users+unsubscribe@googlegroups.com
> <mailto:django-users%2Bunsubscribe@googlegroups.com>
> <javascript:>>.
>     > To post to this group, send email to
> django...@googlegroups.com <mailto:django...@googlegroups.com>
>     <javascript:>
>     > <mailto:django...@googlegroups.com
> <mailto:django...@googlegroups.com> <javascript:>>.
>     > Visit this group at
> https://groups.google.com/group/django-users
> <https://groups.google.com/group/django-users>
>     <https://groups.google.com/group/django-users
> <https://groups.google.com/group/django-users>>.
>     > To view this discussion on the web visit
>     >
> https://groups.google.com/d/msgid/django-users/98c44317-0993-4d41-9d25-33f8a9821dce%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/98c44317-0993-4d41-9d25-33f8a9821dce%40googlegroups.com>
>    
> <https://groups.google.com/d/msgid/django-users/98c44317-0993-4d41-9d25-33f8a9821dce%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/98c44317-0993-4d41-9d25-33f8a9821dce%40googlegroups.com>>
>
>     >
>    
> <https://groups.google.com/d/msgid/django-users/98c44317-0993-4d41-9d25-33f8a9821dce%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/django-users/98c44317-0993-4d41-9d25-33f8a9821dce%40googlegroups.com?utm_medium=email&utm_source=footer>
>    
> <https://groups.google.com/d/msgid/django-users/98c44317-0993-4d41-9d25-33f8a9821dce%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/django-users/98c44317-0993-4d41-9d25-33f8a9821dce%40googlegroups.com?utm_medium=email&utm_source=footer>>>.
>
>     > For more options, visit
> https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>
>     <https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>>.
>
> --
> You received this message because you are subscribed to the
> Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from
> it, send an email to django-users+unsubscribe@googlegroups.com
> <mailto:django-users%2Bunsubscribe@googlegroups.com>
> <mailto:django-users+unsubscribe@googlegroups.com
> <mailto:django-users%2Bunsubscribe@googlegroups.com>>.
> To post to this group, send email to
> django-users@googlegroups.com
> <mailto:django-users@googlegroups.com>
> <mailto:django-users@googlegroups.com
> <mailto:django-users@googlegroups.com>>.
> Visit this group at
> https://groups.google.com/group/django-users
> <https://groups.google.com/group/django-users>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/dd78c8cf-1892-4740-b3cb-1e384b9bd788%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/dd78c8cf-1892-4740-b3cb-1e384b9bd788%40googlegroups.com>
> <https://groups.google.com/d/msgid/django-users/dd78c8cf-1892-4740-b3cb-1e384b9bd788%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/django-users/dd78c8cf-1892-4740-b3cb-1e384b9bd788%40googlegroups.com?utm_medium=email&utm_source=footer>>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to django-users+unsubscribe@googlegroups.com
> <mailto:django-users%2Bunsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto:django-users@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users
> <https://groups.google.com/group/django-users>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/4facfe44-e50e-e7d4-e41a-1c82775a8f7f%40dewhirst.com.au
> <https://groups.google.com/d/msgid/django-users/4facfe44-e50e-e7d4-e41a-1c82775a8f7f%40dewhirst.com.au>.
>
>
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto:django-users@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CA%2BFDnhJbavWjzyOnBLaTF1-VZTsHXjsjcfpWBEmaMyUsre--hA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CA%2BFDnhJbavWjzyOnBLaTF1-VZTsHXjsjcfpWBEmaMyUsre--hA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/53a58428-bbf0-929e-0811-d95faa54e5fa%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

Re: What happened to CollapsedFieldsets.js?



On Tue, Feb 27, 2018 at 1:28 PM, Alan <alanwilter@gmail.com> wrote:
Hi there,

Well, I used that like 8 years ago. Just wondering what's the current solution for Django 2.0 if one want to collapse fields in forms.


AFAIK is to write your own javascript (or use a third-party library / "toolkit") for doing that. You can see the implementation in the admin as a reference:
 
Thanks in advance,

Alan

--
I'll cycle across Britain in 2018 for a charity, would you consider
Many thanks!
--
Alan Wilter SOUSA da SILVA, DSc
Senior Bioinformatician, UniProt
European Bioinformatics Institute (EMBL-EBI)
European Molecular Biology Laboratory
Wellcome Trust Genome Campus
Hinxton
Cambridge CB10 1SD
United Kingdom
Tel: +44 (0)1223 494588

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEznbznf1TyN-LSr5apdf7LGZJDR9Lco96GcJqMS%3DF4vK7jRQA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BFDnhKz%3Dh2A1i_%2BVn0_pZyvWx6kjEKT9%3DjXKn2jBoDFZuYmmw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re: Static/constant dictionary for get_initial or get_form_kwargs



On Tue, Feb 27, 2018 at 9:10 PM, Bob Glassett <megadrone.bob@gmail.com> wrote:
Hello, 

Trying to bring a django app into production, and I ran into a real headscratcher.

I have a Class based view inherited from create.  When the page reloads, after validation, I check the initial dictionary for a field's value to fill in the queryset for a different field.

I'm getting sporadic errors about one of the fields not found in the initial object.  When I looked closer into the problem, the initial dicitonary matched a form for a totally different model.

If I need to pre-populate the initial dictionary, I override the get_initial and return the dictionary that I want.  I am not setting initial= in the class definition.  Is this the right way to do this task?

I am concerned about a static initial dictionary sticking around.  The base edit class returns a copy of the initial dictionary, but if the initial dicitonary somehow has invalid values in it, I could be seeing this for all my forms.

This is what I did for the initial dictionary:

class UserCreateView(AdminCreateView):

    model=User

    success_url='/portal/accounts/list'

    form_class=PortalUserForm 

    

    def get_form_kwargs(self):

        kwargs = super(UserCreateView, self).get_form_kwargs()

        kwargs.update({'request' : self.request})

        return kwargs 

    

    def get_initial(self):

        return {}


On a ModelForm (unrelated to the form/model above) I was trying to access the self.initial for a particular field, which threw a Key exception.  The initial dictionary passed down on the yellow screen did not match the form or data for the view at all.


kwargs:  



{'initial': {'agency': <Agency: Baldwin>, 'canEnterMealCounts': False, 'canManageCalendar': True, 'canManageCustomerAllergies': True, 'canManageFieldTrips': True, 'canManageSocializations': True, 'canManageSpecialOrders': True, 'canManageSupplies': False, 'canPrintMenus': True, 'chefablesUser': False, 'location': <QuerySet []>, 'phone': PhoneNumber(country_code=1, national_number=2018158136, extension=None, italian_leading_zero=None, number_of_leading_zeros=None, country_code_source=1, preferred_domestic_carrier_code=None)}, 'instance': None, 'prefix': None}


I have no idea where that initial dictionary came from.  My get_form_kwargs looks like this:


    def get_form_kwargs(self):

        kwargs = super(PendingLabelsCreateView, self).get_form_kwargs()

        kwargs.update({'user': self.request.user})

        return kwargs


The direct ancestor doesn;'t have get_form_kwargs defined, and that is defined as such:


class AdminCreateView(LoginRequiredMixin, UserPassesTestMixin, CreateView):


I need to understand where that initial value came from and determine if I have static values where I don't want them.


Maybe they are set in the Form?
 

Thanks in advance



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/db24def3-f8cc-4954-bbd3-72b6ed3fa0d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BFDnh%2BCu3FKXLn_-T7FT34c7bvmsSM6GMxgoM3eN37PuTF1Cg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

GSOC WITH DJANGO

hey, good day guys i am a Django developer thats been building for almost a year now and i am participating with gsoc this year with django, I want to improve my skills, solve problems and build the community too.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5ab71783-19bf-479d-8f22-5eacf1d21b5b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Is there a way to make a field both foreignKey or allow user to fill it in themself?



On Wed, Feb 28, 2018 at 12:46 AM, Mike Dewhirst <miked@dewhirst.com.au> wrote:
On 28/02/2018 1:58 PM, Alexander Joseph wrote:
Sorry, I think my question was confusing. What I want to do is allow the user to either select a user from a drop down list of users from the ForeignKey in the Users model, or if there is not a user suitable to select in the drop down, they can manually type in a name themselves.

In the same field that would require a hack of the first water.  Way above my pay grade. For me, anything which equates an existing user object with a string typed in is likely to end in misery.

Otherwise a separate CharField for the typed in name and null=True and blank=True in the ForeignKey ought to work.

Mike

Indeed, what you can also do - besides the nullable FK, is process that in the Form where if the FK is null and the the charfield is not None, then create a User (maybe non active) with that name and assign it to the FK. 
 


Thanks for your reply



On Tuesday, February 27, 2018 at 7:45:51 PM UTC-7, Mike Dewhirst wrote:

    On 28/02/2018 1:26 AM, Alexander Joseph wrote:
    > Is there a way to make a form field or model field either a foreign
    > key reference or allow the user to fill in something themselves?
    I'm
    > making a fixed assets app and each fixed asset has an owner field,
    > which I would like to assign to an app user if possible, and if not
    > possible be able to fill in a name myself.

    Yes. Make the ForeignKey in the model null=True and blank=True so
    it can
    exist all alone. Then you can limit_choices_to whatever whatever
    suits
    your requirements ...

    https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to
    <https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to>


    hth
    Mike


    >
    > Thanks
    > --
    > You received this message because you are subscribed to the Google
    > Groups "Django users" group.
    > To unsubscribe from this group and stop receiving emails from
    it, send
    > an email to django-users...@googlegroups.com <javascript:>
    > <mailto:django-users+unsubscribe@googlegroups.com <javascript:>>.
    > To post to this group, send email to django...@googlegroups.com
    <javascript:>
    > <mailto:django...@googlegroups.com <javascript:>>.
    > Visit this group at https://groups.google.com/group/django-users
    <https://groups.google.com/group/django-users>.
    > To view this discussion on the web visit
    >
    https://groups.google.com/d/msgid/django-users/98c44317-0993-4d41-9d25-33f8a9821dce%40googlegroups.com
    <https://groups.google.com/d/msgid/django-users/98c44317-0993-4d41-9d25-33f8a9821dce%40googlegroups.com>

    >
    <https://groups.google.com/d/msgid/django-users/98c44317-0993-4d41-9d25-33f8a9821dce%40googlegroups.com?utm_medium=email&utm_source=footer
    <https://groups.google.com/d/msgid/django-users/98c44317-0993-4d41-9d25-33f8a9821dce%40googlegroups.com?utm_medium=email&utm_source=footer>>.

    > For more options, visit https://groups.google.com/d/optout
    <https://groups.google.com/d/optout>.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com <mailto:django-users+unsubscribe@googlegroups.com>.
To post to this group, send email to django-users@googlegroups.com <mailto:django-users@googlegroups.com>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dd78c8cf-1892-4740-b3cb-1e384b9bd788%40googlegroups.com <https://groups.google.com/d/msgid/django-users/dd78c8cf-1892-4740-b3cb-1e384b9bd788%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4facfe44-e50e-e7d4-e41a-1c82775a8f7f%40dewhirst.com.au.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BFDnhJbavWjzyOnBLaTF1-VZTsHXjsjcfpWBEmaMyUsre--hA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re: Fetching next and/or prior objects given an arbitrary ordering

I should add that one solution which I find functional but unattractive is to build a and ordered list of PKs:

things = list(Thing.objects.all().values_list('pk', flat=True))

then find the PK of the current object in that list and look one ahead or behind to get the PK of the neighbor and then fetch it with get(). The problem with this is that it loads an arbitrarily large list of PKs into memory for a job that should have a solution in the form of a database query that the ORM can execute lazily and receiving just one object.

Note that the list of Things above is ordered by Django in respect of the ordering defined in the model meta class, that is, this is an ordered list.

Regards,

Bernd.

On Thursday, 1 March 2018 00:58:58 UTC+11, Julio Biason wrote:
Hi Bernd,

Well, the thing with `get()` is that it will return only one object. What you're looking for is `filter()`.

Say, you want all the things that have an ID after a certain value. So you get `list_of_things = Things.objects.filter(pk__gte=...)`. Now it'll return the list, with all elements after the one you asked.

If you want the previous and next, you can do `list_of_previous = Things.objects.filter(pk__lt=...).limit(1)` and `list_of_next = Things.objects.filter(pk__gt).limit(1)`.

Or something like that ;P

On Wed, Feb 28, 2018 at 8:56 AM, Bernd Wechner <bernd....@gmail.com> wrote:

I'm a bit stumped on this. Given an arbitrary ordering as specified by the ordering meta option:

    https://docs.djangoproject.com/en/2.0/ref/models/options/#ordering

for example:

class Thing(models.Model):
    field1 = ...
    field2 = ...
    field2 = ...
    class Meta:
        ordering = ['field1', '-field2', 'field3']

given an instant of Thing:

thing = Thing.objects.get(pk=...)

how can I get the next Thing after that one, and/or the prior Thing before that one as they appear on the sorted list of Things.

It's got me stumped as I can't think of an easy way to build a filter even with Q object for an arbitrary ordering given there can be multiple fields in ordering and multiple Things can have the same ordering list (i.e. there can be ties - that Django must resolve either arbitrarily or with an implicit pk tie breaker on ordering).

It's got me stumped. I can solve any number of simpler problems just not his generic one (yet).

Ideally I'd not build a list of all objects (waste of memory with large collections), and look for my thing in the list and then pick out the next or prior.

I'd ideally like to fetch it in one query returning the one Thing, or if not possible no worse than returning all Things on side of it and picking off the first or last respectively (even that's kludgy IMHO).

I'm using postgresql and I found a related question here:

    https://dba.stackexchange.com/questions/53862/select-next-and-previous-rows

but would rather stick with the ORM and not even explore SQL (just took a peak to see SQL can be constructed to do it I guess, as if not, the ORM sure won't have a good way of doing it methinks).

I'd have thought this a sufficiently common use case but am perhaps wrong there, with most sites exploiting simple orderings (like date_time or creation say). But I want to build a generic solution that works on any model I write, so I can walk through the objects in the order specified by ordering, without building a list of all of them. In short I want to solve this problem, not reframe the problem or work around it ;-).

Regards,

Bernd.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/751c367c-d5e9-e06b-8f5c-82054f11a9ab%40gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
Julio Biason, Sofware Engineer
AZION  |  Deliver. Accelerate. Protect.
Office: +55 51 3083 8101  |  Mobile: +55 51 99907 0554

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/16f42446-9507-4b07-adc9-31619e8a747b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Fetching next and/or prior objects given an arbitrary ordering

Julio,

Thanks for giving it some though. But I think you misread me a little. I am using the get() only to illustrate that the precondition is, I have a single object. The goal then is find a neighboring object (as defined by the ordering in the model).

Yes indeed, a filter is the first and primary candidate for achieving that, but can you write one that respects an abritrary ordering involving multiple fields, as I exemplified with:

class Thing(models.Model):
     field1
= ...
     field2
= ...
     field2
= ...
     
class Meta:
         ordering
= ['field1', '-field2', 'field3']

Also consider that the ordering thus specified does not stipulate uniqueness in any way, that is many neighboring things in an ordered list may have identical values of field1, field2 and field3.

I'm not sure how Django sorts those ties, but imagine it either defers to the underlying database engine (i.e. uses the sort simply to generate an ORDER BY clause in the SQL for example in the above case:

ORDER BY field1 ASC, field2 DESC, field3 ASC

and lets the underlying database engine define how ties are ordered. Or it could add a pk tie breaker to the end. Matters little, the problem remains: how to find neighbors given an arbitrary ordering and ties.

Can you write a filter clause to do that? I'm curious on that front.

It's easy of course with one sort field with unique values collapsing to an __gt or __lt filter folllowed by first() or last() respectively (not sure that injects a LIMIT clause into the SQL or collects a list and then creams one element from it - I'll test a little I think).

In the mean time, I still feel this has to be a fairly standard use case. It's about browsing objects in a table one by one, with a next and previous button given an ordering specified in the model and no guarantee of uniqueness on the (sort keys).

Regards,

Bernd.

On Thursday, 1 March 2018 00:58:58 UTC+11, Julio Biason wrote:
Hi Bernd,

Well, the thing with `get()` is that it will return only one object. What you're looking for is `filter()`.

Say, you want all the things that have an ID after a certain value. So you get `list_of_things = Things.objects.filter(pk__gte=...)`. Now it'll return the list, with all elements after the one you asked.

If you want the previous and next, you can do `list_of_previous = Things.objects.filter(pk__lt=...).limit(1)` and `list_of_next = Things.objects.filter(pk__gt).limit(1)`.

Or something like that ;P

On Wed, Feb 28, 2018 at 8:56 AM, Bernd Wechner <bernd....@gmail.com> wrote:

I'm a bit stumped on this. Given an arbitrary ordering as specified by the ordering meta option:

    https://docs.djangoproject.com/en/2.0/ref/models/options/#ordering

for example:

class Thing(models.Model):
    field1 = ...
    field2 = ...
    field2 = ...
    class Meta:
        ordering = ['field1', '-field2', 'field3']

given an instant of Thing:

thing = Thing.objects.get(pk=...)

how can I get the next Thing after that one, and/or the prior Thing before that one as they appear on the sorted list of Things.

It's got me stumped as I can't think of an easy way to build a filter even with Q object for an arbitrary ordering given there can be multiple fields in ordering and multiple Things can have the same ordering list (i.e. there can be ties - that Django must resolve either arbitrarily or with an implicit pk tie breaker on ordering).

It's got me stumped. I can solve any number of simpler problems just not his generic one (yet).

Ideally I'd not build a list of all objects (waste of memory with large collections), and look for my thing in the list and then pick out the next or prior.

I'd ideally like to fetch it in one query returning the one Thing, or if not possible no worse than returning all Things on side of it and picking off the first or last respectively (even that's kludgy IMHO).

I'm using postgresql and I found a related question here:

    https://dba.stackexchange.com/questions/53862/select-next-and-previous-rows

but would rather stick with the ORM and not even explore SQL (just took a peak to see SQL can be constructed to do it I guess, as if not, the ORM sure won't have a good way of doing it methinks).

I'd have thought this a sufficiently common use case but am perhaps wrong there, with most sites exploiting simple orderings (like date_time or creation say). But I want to build a generic solution that works on any model I write, so I can walk through the objects in the order specified by ordering, without building a list of all of them. In short I want to solve this problem, not reframe the problem or work around it ;-).

Regards,

Bernd.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/751c367c-d5e9-e06b-8f5c-82054f11a9ab%40gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
Julio Biason, Sofware Engineer
AZION  |  Deliver. Accelerate. Protect.
Office: +55 51 3083 8101  |  Mobile: +55 51 99907 0554

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bf6698d0-9aa5-4251-be69-62fe53afb603%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.