0

Hi I have the following django model:

class Issue(models.Model):
    title = models.CharField(max_length=200)
    date = models.DateTimeField(auto_now=True)
    assignee = models.ForeignKey(User, on_delete=models.CASCADE, related_name='assignee')
    owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='owner', null=True, blank=True)
    description = models.TextField()
    state = models.IntegerField(choices=STATUS_CHOICES, default=1)
    priority = models.IntegerField(choices=RELEVANCE_CHOICES, default=2)
    expired_date = models.DateField(auto_now=False, null=True, blank=True)

and a form which allow a user to create an Issue instance:

class IssueForm(forms.ModelForm):

    class Meta:
        model = Issue
        fields = ('title', 'description', 'assignee', 'state', 'priority', 'expired_date')


    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['title'].label = "Titolo"
        self.fields['description'].label = "Descrizione"
        self.fields['state'].label = "Stato"
        self.fields['priority'].label = "Priorità"
        self.fields['expired_date'].label = "Termine"
        self.fields['expired_date'].widget.attrs.update({'class': 'datepicker'})
        self.fields['assignee'] = forms.MultipleChoiceField(
            choices=self.fields['assignee'].choices,
            widget=forms.CheckboxSelectMultiple,
            label=("Assegnatario")
        )

    def clean(self):
        cleaned_data = super().clean()
        user_id = [i for i in cleaned_data['assignee']]
        cleaned_data['assignee'] = [User.objects.get(id=i) for i in user_id]
        return cleaned_data

I render this form and the field assignee is a checkbox. I would like to be able to choose several assignee for the same issue, but I got an error because the Issue model expect just one User instance
How can I modify my model Issue in order to get more than one user ? Thanks

1 Answer 1

1

you can create a new class and name it Issue_Instance where every Issue Object can have an assignee as a foreign key the problem that the relation is one to many because you have to choose more than one assignee and Django doesn't support the idea of having Array or List of Foreign Keys(I don't know any frame works that do :=) ) so I would suggest creating a new class or make the foreign key relation one-to-many key field read about it it will be very useful to solve your problem

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply! I solved by changing the model in this way: assignee = models.ManyToManyField(User)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.