Subqueries

2011-04-06 Thread bernatbonet
Data Model:
class A(models.model):
   desc: model.CharField()
class B(models.model):
   a: model.ForeignKey('A')
   desc: models.CharField()
I need to do this select in a view:
   select max(num_a) as max_num_a from (select a, count(desc) as
num_a
from B group by a) as x;
I've tried this:
   result =
B.objects.values('a').annotate(num_a=Count('a')).aggregate(Max('num_a'))
And I got this error:
   1064, "You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'FROM (SELECT `B`.a` AS `a`, COUNT(`a' at line 1")
If we only group and not obtain max it works:
   result = B.objects.values('a').annotate(num_a=Count('a'))
and the result is : {'a': 1L, 'num_a': 9}{'a': 2L, 'num_a': 6}{'a':
3L,
'num_a': 4}
I'm not sure what I'm doing wrong, neither if there's other way to
resolve it.
If somebody have been faced with and resolved it, I'll be gratefull if
he can show me how can I start for beating this.
Thanks all.

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



Uso de subconsultas

2011-04-06 Thread bernatbonet
Tengo el siguiente modelo de datos:

class A(models.model):
   desc: model.CharField()

class B(models.model):
   a: model.ForeignKey('A')
   desc: models.CharField()

Tengo una vista que quiere sacar la siguiente select:
   select max(num_a) as max_num_a from (select a, count(desc) as num_a
from B group by a) as x;

He intentado hacer lo siguiente:
   result =
B.objects.values('a').annotate(num_a=Count('a')).aggregate(Max('num_a'))

Esto me da el siguiente error:
   1064, "You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'FROM (SELECT `B`.a` AS `a`, COUNT(`a' at line 1")

Si en lugar de obtener el máximo nos quedamos en la agrupación
funciona:
   result = B.objects.values('a').annotate(num_a=Count('a'))
y nos devuelve: {'a': 1L, 'num_a': 9}{'a': 2L, 'num_a': 6}{'a': 3L,
'num_a': 4}

No se que estoy haciendo mal, ni si hay otra forma de resolver este
problema.
Si alguien se ha encontrado con el mismo problema y lo ha resuelto, le
agradecería mucho que me diera unas pistas para solventarlo.

Muchas gracias por todo.



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



Re: Inline Foreign Key Filter

2011-03-23 Thread bernatbonet
I've been trying doing this:

class ArticuloTallaInline(admin.TabularInline):
model = Articulo_Talla
extra = 0

def get_formset(self, request, obj=None, **kwargs):
self.padre = obj
return super(ArticuloTallaInline, self).get_formset(request, 
obj,
**kwargs)

def formfield_for_foreignkey(self, db_field, request, **kwargs):
print self.padre.tallaje
if db_field.name == "talla":
kwargs["queryset"] = Talla.objects.filter(tallaje=1)
return super(ArticuloTallaInline,
self).formfield_for_foreignkey(db_field, request, **kwargs)

It shows me value of tallaje in parent form but I got an Exception
Value: 'NoneType' object has no attribute 'tallaje'

My idea is do something like this: kwargs["queryset"] =
Talla.objects.filter(tallaje=self.padre.tallaje) but it doesn't work.

Thanks.


On 23 mar, 14:51, bernatbonet <bernatbo...@gmail.com> wrote:
> Hi, I've been trying to solve one probleme, and I'll explain what I
> want to do.
>
> First I'll show my models;
>
> class Tallaje(models.Model):
>         cod = models.CharField(max_length=5)
>         desc = models.CharField(max_length=60)
>
> class Talla(models.Model):
>         tallaje = models.ForeignKey('Tallaje')
>         cod = models.CharField(max_length=5)
>         desc = models.CharField(max_length=60)
>
> class Articulo(models.Model):
>         ref = models.CharField(max_length=15)
>         tallaje = models.ForeignKey('Tallaje')
>
> class Articulo_Talla(models.Model):
>         articulo = models.ForeignKey('Articulo')
>         talla = models.ForeignKey('Talla')
>
> And my admin.py file:
>
> class ArticuloTallaAdmin(admin.TabularInline):
>         model = Articulo_Talla
>         extra = 0
>
> class ArticuloAdmin(admin.ModelAdmin):
>         list_display   = ('ref',)
>         list_filter    = ('ref',)
>         ordering       = ('ref',)
>         search_fields  = ('ref',)
>         fieldsets = (
>                 (None, {'fields': ('ref', 'tallaje')}),
>         )
>         inlines = [ArticuloTallaAdmin]
>
> I want to filter talla select from inline form by the value selected
> in parent field tallaje.
> Every time I change tallaje in parent, tallas in inline form must be
> queryset changed.
>
> I don't know if this can be done in admin.py or I must do it in
> javascript, and I want to know where to start.
>
> Thanks a lot.

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



Inline Foreign Key Filter

2011-03-23 Thread bernatbonet
Hi, I've been trying to solve one probleme, and I'll explain what I
want to do.

First I'll show my models;

class Tallaje(models.Model):
cod = models.CharField(max_length=5)
desc = models.CharField(max_length=60)

class Talla(models.Model):
tallaje = models.ForeignKey('Tallaje')
cod = models.CharField(max_length=5)
desc = models.CharField(max_length=60)

class Articulo(models.Model):
ref = models.CharField(max_length=15)
tallaje = models.ForeignKey('Tallaje')

class Articulo_Talla(models.Model):
articulo = models.ForeignKey('Articulo')
talla = models.ForeignKey('Talla')

And my admin.py file:

class ArticuloTallaAdmin(admin.TabularInline):
model = Articulo_Talla
extra = 0

class ArticuloAdmin(admin.ModelAdmin):
list_display   = ('ref',)
list_filter= ('ref',)
ordering   = ('ref',)
search_fields  = ('ref',)
fieldsets = (
(None, {'fields': ('ref', 'tallaje')}),
)
inlines = [ArticuloTallaAdmin]

I want to filter talla select from inline form by the value selected
in parent field tallaje.
Every time I change tallaje in parent, tallas in inline form must be
queryset changed.

I don't know if this can be done in admin.py or I must do it in
javascript, and I want to know where to start.

Thanks a lot.

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