Este es un ejemplo que tengo, un sitio que organiza a personas en departamentos con pedidos por departamento. Es el mismo concepto que tu problema pero con diferentes modelos. Este ejemplo usa muchos a muchos a través de la tabla.
class Department(models.Model):
slug = models.SlugField(
verbose_name = _(u'Slug'),
help_text = _(u'Uri identifier for this department.'),
max_length=255
)
name = models.CharField(
verbose_name = _(u'Department Name'),
help_text = _(u'The department\'s name.'),
max_length = 255
)
description = models.TextField(
verbose_name = _(u'Description'),
help_text = _(u'Department\'s description')
)
people = models.ManyToManyField(
Person,
through = 'DepartmentPeople',
related_name = 'people',
verbose_name = _(u'People'),
help_text = _(u'People in this Department')
)
order_by = models.IntegerField(
verbose_name = _(u'Ordering Weight'),
help_text = _(u'This item\'s weight within a list.'),
max_length = 255
)
class Meta:
verbose_name = _(u"Department")
verbose_name_plural = _(u"Departments")
ordering = ['order_by',]
def people_list(self):
return [dp.person for dp in DepartmentPeople.objects.filter(department=self).order_by('order')]
def __unicode__(self):
return self.name
Y el modelo a través de:
class DepartmentPeople(models.Model):
person = models.ForeignKey(
Person,
verbose_name = _(u'Person'),
help_text = _(u'Person is a member of this deparment.'),
)
department = models.ForeignKey(
Department,
verbose_name = _(u'Department'),
help_text = _(u'Pseron is a member of this department.'),
)
order = models.IntegerField(
verbose_name = _(u'Order'),
help_text = _(u'What order to display this person within the department.'),
max_length = 255
)
class Meta:
verbose_name = _(u"Department Person")
verbose_name_plural = _(u"Department People")
ordering = ['order',]
def __unicode__(self):
return self.person.first_name + " " + self.person.last_name + " is a member of " + self.department.name + (" in position %d" % self.order)
Y el administrador:
class DepartmentPeopleInline(admin.TabularInline):
model = DepartmentPeople
extra = 1
class DepartmentAdmin(admin.ModelAdmin):
inlines = (DepartmentPeopleInline,)
admin.site.register(Person, PersonAdmin)
admin.site.register(Department, DepartmentAdmin)
SOLICITUD EN COMENTARIO:
Nota: la siguiente es mi PersonAdmin, pero su innecesariamente complicada para este ejemplo. usted podría llegar a funcionar con un simple
class PersonAdmin(admin.ModelAdmin) :
pass
pero esto es lo que estoy usando en mi aplicación:
class PersonForm(forms.ModelForm):
abstract = forms.CharField(
widget=TinyMCE(attrs={'cols': 80, 'rows': 30})
)
class Meta:
model = Person
class PersonAdmin(reversion.VersionAdmin):
form = PersonForm
# The Form Fieldsets
fieldsets = [
(
None,
{
'fields' : [('first_name', 'last_name', 'post_nominal',), 'slug', 'title', 'headshot', 'large_photo', ('email', 'phone',), 'abstract']
},
)
]
# Prepopulated fields
prepopulated_fields = {'slug': ('first_name', 'last_name', 'post_nominal',)}
# Fields that are readonly
#readonly_fields = ('slug',)
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'headshot':
request = kwargs.pop("request", None)
kwargs['widget'] = AdminImageWidget
return db_field.formfield(**kwargs)
return super(PersonAdmin, self).formfield_for_dbfield(db_field, **kwargs)
¿Pongo el código de administrador al lado del registro? lo siento, soy novato en Django –
Voy a probarlo esta noche, parece bueno –
Tienes que poner las definiciones de clase de administrador antes de registrarlas. Edité mi respuesta para mostrar cómo. –