Django - 当领域需要重复时,模型最佳实践 [英] Django - Model best practice when fields need to be repeated

查看:114
本文介绍了Django - 当领域需要重复时,模型最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,其中一些字段可能在同一个对象的0到5次之间重复。

I have a model where some fields could be repeated between 0 and 5 times for the same object.

models.py

models.py:

class FusionTableLayer(models.Model):
    layer_name = models.SlugField(max_length=50)
    condition1 = models.CharField('SQL Query Conditions', max_length=100, blank=True)
    condition2 = models.CharField('SQL Query Conditions', max_length=100, blank=True)
    condition3 = models.CharField('SQL Query Conditions', max_length=100, blank=True)
    condition4 = models.CharField('SQL Query Conditions', max_length=100, blank=True)
    condition5 = models.CharField('SQL Query Conditions', max_length=100, blank=True)
    option1 = models.CharField('SQL Query Conditions', max_length=100, blank=True)
    option2 = models.CharField('SQL Query Conditions', max_length=100, blank=True)
    option3 = models.CharField('SQL Query Conditions', max_length=100, blank=True)
    option4 = models.CharField('SQL Query Conditions', max_length=100, blank=True)
    option5 = models.CharField('SQL Query Conditions', max_length=100, blank=True)
    option6 = models.CharField('SQL Query Conditions', max_length=100, blank=True)
    ...etc

我的问题是创建第二个可以重复的字段的模型,并使用 ForeignKey 来链接它们?

My question is it better to create a second model with the fields that could be duplicated and use a ForeignKey to link them?

models.py

class EzApp(models.Model):
    layer_name = models.SlugField(max_length=50)



class EzAppOptions(models.Model):
    app = models.ForeignKey(EzApp)
    condition = models.CharField('SQL Query Conditions', max_length=100, blank=True)
    option = models.CharField('SQL Query Conditions', max_length=100, blank=True

我知道这样看起来整洁,但我发现它更加复杂编辑以使表单,视图和模板与具有 ForeignKey 关系的第二个模型配合使用。例如,我必须在同一视图中管理两个不同的表单。在这种情况下最好的做法是什么?

I know it looks neat like this, but I found it more complicated to adapt forms, views and template to works with a second models with ForeignKey relationship. For instance, I have to manage two different formsets in the same view. What would be the best practice in this case?

推荐答案

models.py

class EzApp(models.Model):
    layer_name = models.SlugField(max_length=50)

    def __unicode__(self):
        return self.layer_name

class EzAppOptions(models.Model):
    app = models.ForeignKey(EzApp)
    condition = models.CharField('SQL Query Conditions', max_length=100, blank=True)
    option = models.CharField('SQL Query Conditions', max_length=100, blank=True

    def __unicode__(self):
        return self.condition

forms.py / p>

forms.py

class EzAppForm(ModelForm):
    class Meta:
        model = EzApp


OptionFormset = inlineformset_factory(EzApp, EzAppOptions, 
    fields=('condition', 'option'), can_delete=True)

vie ws.py

def view_name(request):
    form = EzAppForm()
    formset = OptionFormset(instance=EzApp())
    return render(request, "page.html", {
        'form': form, 'formset': formset
    })

template.html

<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    {{ formset.as_p }}
    <input type="submit" value="Save"/>
</form>

这篇关于Django - 当领域需要重复时,模型最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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