Friday, January 31, 2014

Pre-populating data in the admin panel

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)

--
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/7e356b70-4ec0-48a3-af02-3ec9c5a836ab%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment