0

I'm learning Django as I go right now, but I'm trying to do something more dynamically.

Basically I've got multiple models defined as so:

class Group(models.Model):
     name = models.CharField(max_length=255)
     type = models.CharField(max_length=255) # Ignore this, is used somewhere else

class User(models.Model):
     name = models.CharField(max_length=255)
     group = models.ForeignKey(Group, verbose_name='group', on_delete=models.CASCADE)

(there are more models than these two)

I feel like writing a different view for each of these models isn't really a Django way to do it, but I'm struggling to find a different way. Basically I want to automatically create forms depending on what fields the model has.

So on /department/ there should be an text input for name and type. But on /user/ there should be an text input for name and an selection for group. All that rather in the same template. If possible ofcourse.

If it isn't possible, what's the best way to do this? Since creating a different template for each model doesn't seem right.

EDIT: I've now got my CreateView working (very basic still). Now I want to create a ListView in a similar fashion. I've seen this issue, Iterate over model instance field names and values in template but it wasn't able to help me out...

forms.py

class ModelCreate(CreateView):
    fields = '__all__'
    template_name = 'polls/models/model_create.html'

    def get_form(self, form_class=None):
        try:
            self.model = apps.get_model('polls', self.kwargs['model'])
        except LookupError:
            raise Http404('%s is not a valid model.' % self.kwargs['model'])

        return super(ModelCreate, self).get_form(form_class)

model_create.html

{% extends 'polls/core.html' %}

{% block content %}
<form method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit">
</form>
{% endblock %}

Would it be possible to create a ListView in a similar fashion? So that I get field names and values dynamically (bit like {{ form }})

MrDikke
  • 121
  • 13
  • best way would be to create differnet views, using model forms https://docs.djangoproject.com/en/2.1/topics/class-based-views/generic-editing/#model-forms – Jibin Mathews Jan 23 '19 at 14:54
  • Have you seen the [ModelForm](https://docs.djangoproject.com/pl/2.1/topics/forms/modelforms/#modelform) and [form rendering](https://docs.djangoproject.com/en/2.1/topics/forms/#the-template)? In this way you could reuse same template changing only the ModelForm in view, like in [FormView](https://docs.djangoproject.com/en/2.1/ref/class-based-views/generic-editing/#formview). – mfrackowiak Jan 23 '19 at 14:55
  • @JibinMathews Wouldn't that still require me to create an Create/Update/Delete view per model? Or isn't there any other way to do it? Maybe like an `class FormCreate(CreateView): model = get_model()` or something along those lines? – MrDikke Jan 23 '19 at 15:13
  • @mfrackowiak I finally had time to make your solution work and it does! Thank you so much! (It's kinda the same as Jibin Mathews I know, so I guess thank you both!) But I've got one question left, would it be possible to create such an view for a ListView? – MrDikke Jan 27 '19 at 18:21

0 Answers0