Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using ManyToMany connections for inlines #188

Open
BedirT opened this issue Feb 2, 2019 · 4 comments
Open

Using ManyToMany connections for inlines #188

BedirT opened this issue Feb 2, 2019 · 4 comments

Comments

@BedirT
Copy link

BedirT commented Feb 2, 2019

Is there any way to use ManytoMany model relations to create inline connected forms? I did achieve using the foreign key but didn't find anything about ManytoMany, if it is not implemented how can I do it manually?

i.e.

models.py

class QLevel(models.Model):
    questions = models.ManyToManyField(Question)
    # qID = models.ForeignKey(Question, on_delete=models.CASCADE)
    level = models.CharField(max_length=16)

views.py

class QLevelInline(LoginRequiredMixin, InlineFormSetFactory):
	model = QLevel
	form_class = questionLevelForm
	factory_kwargs = {'extra': 1, 'max_num': None,
					'can_order': False, 'can_delete': False}

class QuestionCreateView(LoginRequiredMixin, CreateWithInlinesView):
	model = Question
	form_class = questionForm
	inlines = [QLevelInline, QSubjectInline, QMethodInline] 
        #other methods are also there but did not include due to simplicity

	def forms_valid(self, form, inlines):
		form.instance.author = self.request.user
		self.object = form.save()
		for inline in inlines:
			inline.save()
		return super().form_valid(form)

	def get_success_url(self):
		return self.object.get_absolute_url()

Basically it works if I use the fk connected version, but I get error

ValueError at /question/new/
'file.QLevel' has no ForeignKey to 'file.Question'.
@Manoel8485
Copy link

I have also this issue...

@BedirT
Copy link
Author

BedirT commented Sep 23, 2020

It's been so long that I don't even remember which project I used this on ;) Otherwise, I would try to help @Manoel8485

@sdolemelipone
Copy link
Collaborator

@Manoel8485 Have you tried using django.forms.forms.ModelMultipleChoiceField in your regular form? I just used that in a project and it worked well.

@jkaeske
Copy link

jkaeske commented Mar 1, 2024

Did someone ever figure this out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants