0

Let me explain what my problem is in more detail.

First I have a class 'UserInfo' which connected to User class of django.contrib.auth.models like below

models.py

class UserInfo(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    phone = models.CharField(max_length=15,blank=True,unique=True)
    position = models.CharField(max_length=15,blank=True,unique=True)

    class Meta:
        default_permissions = ()

    def __str__(self):
        return self.position

then I wanted to use ModelForm because I can write less codes. The reason why I made CreateNewUser class is that I wanted to let user can see only [username, email, groups, user_permissions] and control those. (to prevent them to select superuser or staff or inactive options)

forms.py

class CreateNewUserInfo(forms.ModelForm):
    class Meta:
        model = UserInfo
        fields = '__all__'

class CreateNewUser(forms.ModelForm):
    class Meta:
        model = User
        fields = ['username', 'email', 'groups', 'user_permissions']

problem happened in here. I wanted to use FormView to use generic view with class typed view so that I can write less codes(concise code). there is attribute named 'form_class' and I couldn't put two different class with it. I wanted to put different two class to one form with generic view.

views.py

class TestView(FormView):
    form_class = CustomForm
    template_name = 'manager/alltoall.html'

    def form_valid(self, form):

At the end, I made new class in forms.py and wrote every field which I need like below.

forms.py

class CustomForm(forms.Form):
    username = forms.CharField(initial='testname',max_length=150)
    email = forms.EmailField()
    phone_number = forms.CharField(max_length=15)
    position = forms.CharField(max_length=15)

views.py

class TestView(FormView):
    form_class = CustomForm
    template_name = 'manager/alltoall.html'

    def form_valid(self, form):
        username = form.cleaned_data['username']
        email = form.cleaned_data['email']
        phone_number = form.cleaned_data['phone_number']
        position = form.cleaned_data['position']

        with transaction.atomic():
            user = User.objects.create(username=username,email=email)
            userinfo = UserInfo.objects.create(user=user,phone=phone_number,position=position)
            userinfo.save()
            user.save()

        return super(TestView, self).form_valid(form)

Is there anyway to use ModelForm and FormView to show two different class in a form at the same time? Additionally, I used transaction like above because I wanted to save data in database with two different class. Is it right approach or Is there any other way to do that more conveniently(or efficiently) with built in functions in django?

Thank you for taking your time to read all. I wonder if it is too long, but I wanted to deliver what I wanted to know exactly. Thank you!

Jayground
  • 1,797
  • 1
  • 17
  • 32
  • See if this helps http://stackoverflow.com/questions/18489393/django-submit-two-different-forms-with-one-submit-button/18489593#18489593 – Rohan Oct 06 '16 at 03:47
  • @Rohan Thanks for reply. I checked link you posted. Well, I wanted to know if there is any way to use FormView or other generic view with two different classes in a form. I found some way to add different form models in a one form at the same with with just function based view. I wanted to keep using class typed views. I made CustomForm with wiritng each Fields. But if I can use Modelform, it would be much convenient with less code because I already set those in models.py. so I would like to know if I can use two different Modelform and put those in the same form with FormView(generic view) – Jayground Oct 06 '16 at 04:27
  • In Summery, I wanted to use two or multiple ModelForm and show those at the same time with generic view like FormView. – Jayground Oct 06 '16 at 04:28

0 Answers0