Skip to content

Chained selection not working in admin #22

Closed
drlinux opened this Issue Aug 2, 2011 · 4 comments

3 participants

@drlinux
drlinux commented Aug 2, 2011

There is a little problem with third-level(?) models (or many times related models) in admin area with django-smart-selects plugin. I don't know what does matter :)

This my colors models

from django.db import models

class Colors(models.Model):
color_name = models.CharField(max_length=50)

def __unicode__(self):
    return self.color_name

This my Cars models

from django.db import models

class Cars(models.Model):
car_model = models.CharField(max_length=50)
car_colors = models.ManytoManyField(Colors, related_name='Car Colors')

def __unicode__(self):
    return self.car_model

O.K. Let's see my CarsData Model.

This my CarsData models

from django.db import models

class CarsData(models.Model):
car_barcode= models.CharField(max_length=50)
available_color = ChainedForeignKey(
Cars,
chained_field="car_model",
chained_model_field="car_colors",
show_all=False,
auto_choose=True
)

def __unicode__(self):
    return self.car_barcode

My admin.py looks like that:

from django.contrib import admin
from django import forms
from myapp.models import *

class CarsDataAdminForm(forms.ModelForm):

class Meta:
    model = CarsData
def __init__(self, *arg, **kwargs):
    super(CarsDataAdminForm, self).__init__(*arg, **kwargs)
    self.fields['available_color'].choices =  [(csc.id,csc.car_colors) for csc in Cars.objects.all()]

class CarsDataAdmin(admin.ModelAdmin):
form = CarsDataAdminForm

admin.site.register(CarsData,CarsDataAdmi
n)
Is there anyway to show in the ChoiceField 'just' color_name field datas? I see just car_model because i have to set it :

def unicode(self):
return self.car_model
Can you please give me an example?

Many thanks!

@drlinux
drlinux commented Aug 2, 2011

any help please?

@drlinux
drlinux commented Aug 4, 2011

Please next time if you write something don't give it. Because who uses this plugin also waiting a support. No support, sucks!

@drlinux drlinux closed this Aug 4, 2011
@drlinux drlinux reopened this Aug 11, 2011
@SalahAdDin

I have this error too.
I would put a report that problem, but it seems no help.
Well, i will put.

Here too.

My code in models is :

#Model
class Person(models.Model):
    name = models.CharField(max_length=20, help_text="Tipo de Persona que se inscribe", verbose_name="Nombre del Tipo")

    class Meta:
        verbose_name = "Tipo de Persona"

    def __str__(self):
        return self.name

class MemberShip(models.Model):
    title = models.CharField(max_length=255, help_text="Nombre del tipo de afiliación.",verbose_name="Título")
    #type_person = models.CharField(max_length=255, choices=AFFILIATION_PERSON_TYPE, verbose_name='Tipo de Persona',)
    type_person = models.ForeignKey(Person, verbose_name="Tipo de Persona", related_name="membership")
    descript = models.CharField(max_length=500, help_text="Descripción del tipo de afiliación.", verbose_name="Descripción")
    value = models.DecimalField(decimal_places=2, max_digits=10, verbose_name='Cuota', help_text="Cuota de afiliación.")

    class Meta:
        #description = 'Modelo Membresía'
        verbose_name = "Membresía"

    def __str__(self):
        return self.title

#Afiliacion
class Affilliation(models.Model):
    person = models.ForeignKey(Person, verbose_name="Tipo de Persona", related_name="persona")
    affiliation = ChainedForeignKey(
        MemberShip,
        chained_field="persona",
        chained_model_field="name",
        show_all=False,
        auto_choose=True,
        verbose_name='Membresía',
        #related_name='affiliation'
    )

And my admin code is very simple:

class AffilliationAdmin(admin.ModelAdmin):
    fieldsets = [
        ('Afiliación', {
            'fields': ['person', 'affiliation', 'affiliates', 'is_active'],
        }),
        ('Fechas', {
            'fields': ['date_in', 'date_renew'],
        }),
    ]
    list_display = ('affiliation', 'affiliates', 'is_active', 'date_prein', 'date_in', 'date_renew', 'date_end')
    list_filter = ('affiliation', 'affiliates', 'is_active')
    list_editable = ('affiliation',)
    search_fields = ('affiliation', 'affiliates', 'is_active', 'date_in',)

    def date_end(self):
        return self.date_in + timedelta(days=365)

    date_end.short_description = "Fecha de Expiración"
    date_end.allow_tags = True

admin.site.register(Affilliation, AffilliationAdmin)

As you can see all is well, but, if i try use ChainedField, well, in the django-admin i give this:
problemasconsmarts

In Juridica i have four MemberShips child's, but in this select box they aren't.

In a form this field works?

@SalahAdDin

I don't have this error anymore, please close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.