Django - Model best practice when fields need to be repeated

一笑奈何 提交于 2020-01-01 19:00:13

问题


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

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

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

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?


回答1:


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

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


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

views.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>



回答2:


Take a look at ForeignKey and inline formsets for dealing with situations like this.



来源:https://stackoverflow.com/questions/15166828/django-model-best-practice-when-fields-need-to-be-repeated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!