Django管理多个子集 [英] Django admin many to many subset
问题描述
# models.py
class研讨会(models.Model):
title = models.CharField(max_length = 128,unique = True)
start_date = models.DateField(db_index = True)
end_date = models.DateField(db_index = True)
类事件(models.Model):
title = models.CharField(max_length = 128)
start_date = models.DateTimeField(db_index = True)
end_date = models.DateTimeField(db_index = True)
seminar = models.ForeignKey('Seminar')
class注册(models.Model):
name = models.CharField(max_length = 128)
first_name = models.CharField(max_length = 128)
seminar = models.ForeignKey('Seminar')
events = models.ManyToManyField('Event',null = True)
#admin.py
class EventInline(admin.TabularInline):
model =事件
c lass SeminarAdmin(admin.ModelAdmin):
list_display =('title','start_date','end_date')
inlines = [
EventInline,
]
class RegistrationAdmin(admin.ModelAdmin):
list_display =('seminar','name','first_name')
如您所见,每个研讨会可能会有几个事件,从研讨会
admin添加为内联条目。
我的问题是注册,因为它们是:
- 与研讨会相关
-
- 可以订阅这个研讨会的几个活动
当然,管理员列出所有事件,而不是与研讨会相关的子集,所以:
- 是否可以从admin在
事件
注册 M2M - >适当或应该将这两个模型相互联系起来方式?
谢谢!
好吧,刚才在这里询问之前,我用Django的 auth.User
模型,通过使用不同的表单来添加/更改视图。我希望有一个更好的解决方案,显然不是。
所以这里是我做的:
#models.py
/ pre>
class注册(models.Model):
#[...] - 添加表单排除它,应该允许空白事件字段
events = models.ManyToManyField('Event',blank = True,null = True)
#admin.py
class RegistrationAdmin(admin.ModelAdmin):
change_form = RegistrationChangeForm
def get_form(self,request,obj = None,** kwargs):
defaults = {}
如果obj为None:
defaults.update(exclude =('events',))
else:
defaults.update(form = self.change_form)
defaults.update(kwargs)
return super (RegistrationAdmin,self).get_form(request,obj,** defaults)
#forms.py
class RegistrationChangeForm(forms.ModelForm):
class Meta :
model =注册
exclude =('seminar' )
def __init __(self,* args,** kwargs):
super(RegistrationChangeForm,self).__ init __(* args,** kwargs)
self.fields ['events']。choices = Event.objects.filter(seminar = kwargs.get('instance')。seminar).values_list('pk','title')
所以,当添加我们只是忽略事件(注册被保存为空事件),然后,更改事件列表可以基于以前定义研讨会被忽略(因为我们无法重新加载相关的事件列表)。
I'm trying to integrate in Django admin the next three related models:
# models.py class Seminar(models.Model): title = models.CharField(max_length=128, unique=True) start_date = models.DateField(db_index=True) end_date = models.DateField(db_index=True) class Event(models.Model): title = models.CharField(max_length=128) start_date = models.DateTimeField(db_index=True) end_date = models.DateTimeField(db_index=True) seminar = models.ForeignKey('Seminar') class Registration(models.Model): name = models.CharField(max_length=128) first_name = models.CharField(max_length=128) seminar = models.ForeignKey('Seminar') events = models.ManyToManyField('Event', null=True) # admin.py class EventInline(admin.TabularInline): model = Event class SeminarAdmin(admin.ModelAdmin): list_display = ('title', 'start_date', 'end_date') inlines = [ EventInline, ] class RegistrationAdmin(admin.ModelAdmin): list_display = ('seminar', 'name', 'first_name')
As you can see, each seminar may have several events, added from the
Seminar
admin as inline entries.My problem is with registrations, since they are:
- related to a seminar
- can "subscribe" to several events of this seminar
Of course, the admin lists all events and not the subset related to the seminar, so:
- is it possible to achieve this from the admin (with as low tweaks as possible)?
- is the
Registration
M2M onEvent
appropriate or should I relate this two models in a different way?
Thanks!
Well so, just before asking here, I did something similar to Django's auth.User
model by using different forms for add/change views. I hoped there was a better solution, obviously not.
So here is what I did:
# models.py
class Registration(models.Model):
# [...] - add form excludes it, should allow blank on events field
events = models.ManyToManyField('Event', blank=True, null=True)
# admin.py
class RegistrationAdmin(admin.ModelAdmin):
change_form = RegistrationChangeForm
def get_form(self, request, obj=None, **kwargs):
defaults = {}
if obj is None:
defaults.update(exclude=('events',))
else:
defaults.update(form=self.change_form)
defaults.update(kwargs)
return super(RegistrationAdmin, self).get_form(request, obj, **defaults)
# forms.py
class RegistrationChangeForm(forms.ModelForm):
class Meta:
model = Registration
exclude = ('seminar',)
def __init__(self, *args, **kwargs):
super(RegistrationChangeForm, self).__init__(*args, **kwargs)
self.fields['events'].choices = Event.objects.filter(seminar=kwargs.get('instance').seminar).values_list('pk','title')
So, when adding we simply ignore events (registration is saved with nulled events) and then, on change a list of events can be selected based on the previously defined seminar which is then ignored (since we can't reload the related events list).
这篇关于Django管理多个子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!