Monday, February 3, 2014

Re: Pre-populating data in the admin panel



Le vendredi 31 janvier 2014 09:22:46 UTC+1, kerviel patrice a écrit :
In the Django Admin I want to populate fields for a foreign key record when I click the add (+) button
I tried with formfield_for_foreignkey but it does not work

model

class Property(models.Model):      name = models.CharField(_('name'), max_length=50)      description = models.TextField(_('description'), blank=True)      class Physic(models.Model):      name = models.ForeignKey(Property, verbose_name=_('name'), null=True, blank=True,)      lapropriete = models.CharField(_('property'), max_length=100)      class UniteProperty2(models.Model):      name = models.ForeignKey(Material, verbose_name=_('name'))                                       nature_unit = models.ForeignKey(Property, verbose_name=_('category'))                         choix = models.ForeignKey(Physic, verbose_name=_('properties'), null=True, blank=True,  related_name='UniteProperty2_choix') 

forms

def formfield_for_foreignkey(self, db_field, request, **kwargs):
 
 
        if request.user.is_superuser:
            if db_field.name == 'nature_unit':
                qs = Property.objects.all()
                for index in enumerate(qs):
                    print 'index', index
                kwargs['initial'] = qs
                   
                 
                return db_field.formfield(**kwargs)
         
            if db_field.name == 'choix':
                qs1 = Physic.objects.all()
                for index in enumerate(qs1):
                    print 'index', index
                kwargs['initial'] = qs1
                return db_field.formfield(**kwargs)
 
             
        return super(UniteProperty2Inline, self).formfield_for_foreignkey(db_field, request, **kwargs)

            
and index :

index (0, <Property: Electrical>)
index (1, <Property: Environment>)
index (2, <Property: Mass composition>)
index (3, <Property: Mechanical>)
index (4, <Property: Optical>)
index (5, <Property: Thermal>)


index (0, <Physic: ------Electrical Property------>)
index (1, <Physic: Electrical Resistivity (Ohms.cm)>)
index (2, <Physic: ------Environment Property------>)
index (3, <Physic: Energy primary production (J.kg-1)>)
index (4, <Physic: CO2 primary production (kg.CO2.kg-1)>)
index (5, <Physic: ----Mass composition Property---->)
index (6, <Physic: Al (% massique)>)
index (7, <Physic: ------Mecanical Property------>)
index (8, <Physic: Vickers Hardness (GPa)>)
index (9, <Physic: Elasticity Modulus (GPa)>)
index (10, <Physic: Young Modulus (GPa)>)
index (11, <Physic: Shear Modulus (GPa)>)
index (12, <Physic: Maximum Stress (GPa)>)
index (13, <Physic: ------Optical Property------>)
index (14, <Physic: Refractive Index>)
index (15, <Physic: Emissivity (-)>)
index (16, <Physic: ------Thermal Property------>)
index (17, <Physic: Thermal Conduct (W.m-1.K-1)>)
index (18, <Physic: Thermal diffusivity (m2.s-1)>)

I just want each category is associated has a property

a pre-Chosen dropdown on 'Mechanical' has an associated pre-Chosen dropdown on vickers hardness and Aunsi on

13 or drop-down lists for categories and 13 for properties


how to do ??


       

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ddc0f923-02f0-466f-b800-c41c5f097763%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment