Re: Annotate an object with the value of a filtered, related object's attribute

2017-05-21 Thread Yo'av Moshe
Thanks, never heard of Subqueries before! It's time to upgrade to Django 
1.11 I guess.

Performance-wise, do you know if it's any different than running over my 
Adult objects and for each one of them running a separate query looking for 
their oldest son & daughter? Or is it the same and it just looks better?

Thanks again.



On Sunday, 21 May 2017 17:53:25 UTC+3, Simon Charette wrote:
>
> Hello Yo'av,
>
> You'll want to use subqueries for this[0].
>
> from django.db.models import OuterRef, Subquery
>
> children = 
> Child.objects.filter(adult=OuterRef('pk')).order_by('dob').values('dob')
>
> Adult.objects.annotate(
> oldest_son_dob=Subquery(children.filter(gender='M')[:1]),
> oldest_daughter_dob=Subquery(children.filter(gender='F')[:1]),
> )
>
> Note that I haven't tried the above code myself so it might required 
> adjustments.
>
> Cheers,
> Simon
>
> [0] 
> https://docs.djangoproject.com/en/1.11/ref/models/expressions/#subquery-expressions
>
> Le dimanche 21 mai 2017 10:41:44 UTC-4, Yo'av Moshe a écrit :
>>
>> Hey Djangoists!
>> I can't get my head around this and I'm not sure if it's even possible.
>>
>> Let's say I have an "Adult" object and a "Child" object. Every Child 
>> belongs to an Adult, and has a gender which is either "M" or "F", and also 
>> a "dob" field with their date of birth. I want to get a list of all adults 
>> annotated with the dob of their oldest son, and the dob of their oldest 
>> daughter.
>>
>> How am I to do this?
>>
>> I tried something like this:
>> Adult.objects.annotate(
>>oldest_son_dob=Case(
>>When(children__gender="M", then=F('children__dob')),
>>default=None,
>>output_field=DateField(),
>>)
>> )
>>
>> # ... same for daughter
>>
>>
>> but I'm not sure where to tell Django that I only want it to pick the 
>> oldest child, and so right now it duplicates the adult object for every 
>> child it has.
>>
>> Does Django support this kind of query?
>>
>> I'm using PosgresSQL FWIW.
>>
>> Thank you so much
>>
>> Yo'av
>>
>

-- 
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+unsubscr...@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/b3a4222d-622b-4399-a704-8d9961d23c36%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Annotate an object with the value of a filtered, related object's attribute

2017-05-21 Thread Yo'av Moshe
Hey Djangoists!
I can't get my head around this and I'm not sure if it's even possible.

Let's say I have an "Adult" object and a "Child" object. Every Child 
belongs to an Adult, and has a gender which is either "M" or "F", and also 
a "dob" field with their date of birth. I want to get a list of all adults 
annotated with the dob of their oldest son, and the dob of their oldest 
daughter.

How am I to do this?

I tried something like this:
Adult.objects.annotate(
   oldest_son_dob=Case(
   When(children__gender="M", then=F('children__dob')),
   default=None,
   output_field=DateField(),
   )
)

# ... same for daughter


but I'm not sure where to tell Django that I only want it to pick the 
oldest child, and so right now it duplicates the adult object for every 
child it has.

Does Django support this kind of query?

I'm using PosgresSQL FWIW.

Thank you so much

Yo'av

-- 
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+unsubscr...@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/23ef9a3e-36f6-4c70-a3ea-3ff102db2e3f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Django removes fields from admin's list

2010-11-10 Thread Yo'av Moshe
Hey,
I'm using Django's admin interface to list, edit and add objects to my
application.

When I start my Django application it all works fine, but after a
while the list view goes crazy: first the checkboxes disappear, then
the first field and later on the second one. Sometimes it goes even
further. This is really bad since because I have no checkboxes I can't
use the admin actions at all.

I can't find anything similar on the web, though my application is
very generic and I'm not doing anything special with my views or
something like that.

I'm not even sure where to start searching. Why would the fields
disappear that way?

Note that after refreshing the page for quite a few times, sometimes
the fields and the checkboxes return.

Your help is appreciated,
Yo'av

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Re: Making a m2m field required

2010-11-08 Thread Yo'av Moshe
O thank you! I didn't know about self.data! That's exactly what I
was looking for.

It works now. Thanks!

Yo'av

On Nov 8, 4:22 pm, Martin Ostrovsky <martin.ostrov...@gmail.com>
wrote:
> What about checking the raw POST for existence of data? So in your
> form, checking self.data.has_key('some_key')
>
> On Nov 8, 9:15 am, "Yo'av Moshe" <bje...@gmail.com> wrote:
>
>
>
> > Hey,
>
> > I tried this method with no success.
>
> > Adding .clean to my Article's ModelForm and checking it's
> > self.instance.tags.all(), gives me this ValueError:
> > 'Article' instance needs to have a primary key value before a many-to-
> > many relationship can be used.
>
> > Actually this seems right, since if my Article isn't even saved yet,
> > how can I check it's tags? They don't even exist AFAIK.
>
> > So I thought the way to go was by by checking the form it self. The
> > problem is that Article's ModelForm .clean_data doesn't include my
> > Tag's form, since it's included inline as a seperated form. If I try
> > to use .clean_data on Tag's inline ModelForm it actually works - I can
> > count and validate whatever the user wrote in the Tag's form - but
> > only if the user typed *something*, which is exactly what I'm trying
> > to validate. If the user didn't put anything in Tag's inline ModelForm
> > - .clean_data doesn't get called at all, and no validation is being
> > done.
>
> > I hope I was understood, it's a bit of a mess...
>
> > Is there anyway to go from here? Did I use .clean correctly?
>
> > Thank you for your kind help,
> > Yo'av
>
> > On Nov 7, 12:53 am, Shawn Milochik <sh...@milochik.com> wrote:
>
> > > Add a clean() method to your ModelForm, and raise a forms.ValidationError 
> > > if self.instance.whatever.all().count() == 0.
>
> > > Shawn

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Re: Making a m2m field required

2010-11-08 Thread Yo'av Moshe
Hey,

I tried this method with no success.

Adding .clean to my Article's ModelForm and checking it's
self.instance.tags.all(), gives me this ValueError:
'Article' instance needs to have a primary key value before a many-to-
many relationship can be used.

Actually this seems right, since if my Article isn't even saved yet,
how can I check it's tags? They don't even exist AFAIK.

So I thought the way to go was by by checking the form it self. The
problem is that Article's ModelForm .clean_data doesn't include my
Tag's form, since it's included inline as a seperated form. If I try
to use .clean_data on Tag's inline ModelForm it actually works - I can
count and validate whatever the user wrote in the Tag's form - but
only if the user typed *something*, which is exactly what I'm trying
to validate. If the user didn't put anything in Tag's inline ModelForm
- .clean_data doesn't get called at all, and no validation is being
done.

I hope I was understood, it's a bit of a mess...

Is there anyway to go from here? Did I use .clean correctly?

Thank you for your kind help,
Yo'av

On Nov 7, 12:53 am, Shawn Milochik  wrote:
> Add a clean() method to your ModelForm, and raise a forms.ValidationError if 
> self.instance.whatever.all().count() == 0.
>
> Shawn

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Making a m2m field required

2010-11-06 Thread Yo'av Moshe
Hey,
I'm getting frustrated with this...

Is there any way to make a Many-to-Many field required?
So that an object must have *at least one* relation (i.e. Article must
have at least one Tag)?

I'm showing Tag as an Inline in Article's from in the admin panel, but
I can't find how to force the user to enter at least one Tag.

Is it possible?

Thank you,
Yo'av.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



checkboxes disappearing in the admin panel?

2010-10-20 Thread Yo'av Moshe
Hey,
I'm seeing some very odd behavior in Django's admin panel.

I'm using the admin panel to list about 3500 objects of "people". The
list view shows about 17 fields.
I'm using the checkboxes to perform certain admin actions I wrote.

This all works great, usually.

Sometimes, when I'm looking at the list, there are no checkboxes,
which means I can't select anything to perform any action.
The only way I found to restore the checkboxes is by restarting the
Django process.

Did anyone see this odd behavior? Is there any reason Django is not
showing my checkboxes?

Thank you.

Yo'av

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.