一对多内联选择与django管理员 [英] one-to-many inline select with django admin

查看:105
本文介绍了一对多内联选择与django管理员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个标准的多对一关系。有一些字段,但为了我们的目的,相关模型是:

  class Class(models.Model): 
name = models.CharField(max_length = 128)

class学生(models.Model):
class = models.ForeignKey(Class)
name = models。 CharField(max_length = 128)
address = models.CharField(max_length = 128)
#...等

我创建了一个管理员,它的效果很好。当我编辑学生时,它甚至能够自动设置班级。然而,当我去创建/编辑一个类时,我所得到的只是一个输入框。



有没有办法添加一个方块/可以从Class admin页面添加为Class的成员?我可以内联一个表格,但那就是创造新的学生。我已经有我所有的学生创建,只是寻找一个快速的方法来添加多个现有的学生到不同的类'。

解决方案

这是Luke Sneeringer建议的定制形式解决方案。无论如何,由于缺少开箱即用的Django解决方案(相当自然而且很常见)的问题,我感到惊讶。我是否缺少某些东西?

  from django import forms 
from django.db import models
from django。 contrib import admin

class Foo(models.Model):
pass

class Bar(models.Model):
foo = models.ForeignKey( Foo)

class FooForm(forms.ModelForm):
class Meta:
model = Foo

bars = forms.ModelMultipleChoiceField(queryset = Bar。 object.all())

def __init __(self,* args,** kwargs):
super(FooForm,self).__ init __(* args,** kwargs)
如果self.instance:
self.fields ['bars']。initial = self.instance.bar_set.all()

def save(self,* args,** kwargs) :
#FIXME:'commit'参数不处理
#TODO:将重新分配到事务
#注意:以前分配的Foos静默重置
instance = super(FooForm,self ).save(commit = False)
self.fields ['bars']。initia l.update(foo = None)
self.cleaned_data ['bars']。update(foo = instance)
return instance

class FooAdmin(admin.ModelAdmin):
form = FooForm


I have a standard many-to-one relationship set up. There are a bunch of fields, but for our purposes here, the relevant model is:

class Class(models.Model):
    name = models.CharField(max_length=128)

class Student(models.Model):
    class = models.ForeignKey(Class)
    name = models.CharField(max_length=128)
    address = models.CharField(max_length=128)
    # ...etc

I created an admin, and it works great. it even automatically has the ability for me to set the Class when I am editing a Student. However, when I go to create/edit a Class, all I get is an input box for the name.

Is there a way to add a box/field where Students can be added as members of Class from the Class admin page? I can make a form inline, but that is to create new Students. I already have all my Students created and am just looking for a quick method to add multiple existing Students to different Class'.

解决方案

Here is "custom form" solution as Luke Sneeringer suggested. Anyway, I'm suprised by absence of out-of-the-box Django solution to this (rather natural and probably common) problem. Am I missing something?

from django import forms
from django.db import models
from django.contrib import admin

class Foo(models.Model):
    pass

class Bar(models.Model):
    foo = models.ForeignKey(Foo)

class FooForm(forms.ModelForm):
    class Meta:
        model = Foo

    bars = forms.ModelMultipleChoiceField(queryset=Bar.objects.all())

    def __init__(self, *args, **kwargs):
        super(FooForm, self).__init__(*args, **kwargs)
        if self.instance:
            self.fields['bars'].initial = self.instance.bar_set.all()

    def save(self, *args, **kwargs):
        # FIXME: 'commit' argument is not handled
        # TODO: Wrap reassignments into transaction
        # NOTE: Previously assigned Foos are silently reset
        instance = super(FooForm, self).save(commit=False)
        self.fields['bars'].initial.update(foo=None)
        self.cleaned_data['bars'].update(foo=instance)
        return instance

class FooAdmin(admin.ModelAdmin):
    form = FooForm

这篇关于一对多内联选择与django管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆