简体   繁体   English

Django多模型最佳实践

[英]Django multi model best practice

Basically what I'm trying to achieve is a multi-model django app where different models take advantage of the same views. 基本上我想要实现的是一个多模型django应用程序,其中不同的模型利用相同的视图。 For example I've got the models 'Car' 'Make' 'Model' etc and I want to build a single view to perform the same task for each, such as add, delete and edit, so I don't have to create a seperate view for add car, ass make etc. I've built a ModelForm and Model object for each and would want to create a blank object when adding and a pre-populated form object when editing (through the form instance arg), with objects being determined via url parameters. 例如,我有模型'Car''Make''Model'等,我想构建一个单独的视图来为每个执行相同的任务,例如添加,删除和编辑,所以我不必创建添加汽车,屁股等的单独视图。我为每个人构建了一个ModelForm和Model对象,并且在编辑时(通过表单实例arg)添加和预先填充的表单对象时想要创建一个空白对象,通过url参数确定的对象。

Where I'm stuck is that I'm not sure what the best way to so this is. 我被困的地方是,我不确定最好的方法是什么。 At the moment I'm using a load of if statements to return the desired object or form based on parameters I'm giving it, which get's a bit tricky when different forms need specifying and whether they need an instance or not. 目前我正在使用if语句加载来根据我给出的参数返回所需的对象或表单,当不同的表单需要指定以及是否需要实例时,这会变得有点棘手。 Although this seems to be far from the most efficient way of achieving this. 虽然这似乎远非实现这一目标的最有效方式。

Django seems to have functions to cover most repetitive tasks, is there some magic I'm missing here? Django似乎有覆盖大多数重复任务的功能,这里有一些我不知道的魔法吗?

edit - Here's an example of what I'm doing with the arguments I'm passing into the url: 编辑 - 这是我正在使用我传递给url的参数的一个例子:

def edit_object(request, object, id):
    if(object==car):
        form = carForm(instance = Car.objects.get(pk=id)
    return render(request, 'template.html', {'form':form})

What about using Class Based Views ? 那么使用基于类的视图呢? Using CBVs is the best way in Django to make reusable code. 使用CBV是Django制作可重用代码的最佳方式。 For this example maybe it can be a little longer than function based views, but when the project grows up it makes the difference. 对于这个例子,它可能比基于函数的视图长一点,但是当项目成长时,它会产生不同。 Also remember "Explicit is better than implicit". 还要记住“明确比隐含更好”。

urls.py urls.py

# Edit
url(r'^car/edit/(?P<pk>\d+)/$', EditCar.as_view(), name='edit-car'),
url(r'^make/edit/(?P<pk>\d+)/$', EditMake.as_view(), name='edit-make'),

# Delete
url(r'^car/delete/(?P<pk>\d+)/$', DeleteCar.as_view(), name='delete-car'),
url(r'^make/delete/(?P<pk>\d+)/$', DeleteMake.as_view(), name='delete-make'),

views.py views.py

class EditSomethingMixin(object):
    """Use Mixins to reuse common behavior"""
    template_name = 'template-edit.html'


class EditCar(EditSomethingMixin, UpdateView):
    model = Car
    form_class = CarForm


class EditMake(EditSomethingMixin, UpdateView):
    model = Make
    form_class = MakeForm


class DeleteSomethingMixin(object):
    """Use Mixins to reuse common behavior"""
    template_name = 'template-delete.html'


class DeleteCar(DeleteSomethingMixin, DeleteView):
    model = Car


class DeleteMake(DeleteSomethingMixin, DeleteView):
    model = Make

Just pass your class and form as args to the method then call them in the code. 只需将您的类和表单作为args传递给方法,然后在代码中调用它们。

def edit_object(request, model_cls, model_form, id):
    form = model_form(instance = model_cls.objects.get(pk=id)
    return render(request, 'template.html', {'form':form})

then just pass in the correct classes and forms in your view methods 然后在视图方法中传入正确的类和表单

def edit_car(request,id):
    return edit_object(request, Car, CarForm, id)

each method knows what classes to pass, so you eliminate the if statements. 每个方法都知道要传递的类,因此您可以消除if语句。

urls.py urls.py

url(r'^car/delete/(?<pk>\d+)/', edit, {'model': Car})
url(r'^make/delete/(?<pk>\d+)/', edit, {'model': Make})

views.py views.py

def edit(request, id, model):
    model.objects.get(id=id).delete()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM