Pre-populating data in the admin panel

233 views
Skip to first unread message

kerviel patrice

unread,
Jan 31, 2014, 3:22:46 AM1/31/14
to django...@googlegroups.com
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 db_field.name == 'nature_unit':
            kwargs['queryset'] = Property.objects.all()
            return db_field.formfield(**kwargs)
        if db_field.name == 'choix':
            kwargs['queryset'] = Physic.objects.all()
            return db_field.formfield(**kwargs)
            
 
        return super(UniteProperty2Inline, self).formfield_for_foreignkey(db_field, request, **kwargs)

shmengie

unread,
Jan 31, 2014, 9:39:12 AM1/31/14
to django...@googlegroups.com
Add your foreign key table to the admin.py and register it.  Admin will magically add the (+) button.
Or add it inline and have it all on the same page.
Depends on which is right for your application.

admin.py:

from project.models import *

class PropertyAdmin(admin.ModelAdmin):
    pass
admin.site.register(Property, PropertyAdmin)

class PhysicAdmin(admin.ModelAdmin):
    pass
admin.site.register(Physic, PhysicAdmin

kerviel patrice

unread,
Feb 3, 2014, 3:06:45 AM2/3/14
to django...@googlegroups.com


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


       

Reply all
Reply to author
Forward
0 new messages