ubins
February 23, 2023, 6:11am
1
Here i want to put multiple values in one model field that are sent to it via forms
In forms i use
addinex = forms.MultipleChoiceField ( widget = forms.CheckboxSelectMultiple, choices = listt )
What type of model field do i need to put in the models , so that i can save the data
You can use another model with Many to Many relation and can store there id’s after the object is created. You have to handle this from the related views and by creating a model for the addinex field it will be easier to manipulate data as well instead of storing the whole different values within one text field in model.
ubins
February 23, 2023, 7:25am
3
Can you show any code that follows the process you described ?
I have some code reference that you can take which I’ve used in one of my projects.
For the models part
class AttachmentFiles(Model):
file = models.FileField(upload_to="attachment")
class ABC(Model):
....
attachments = models.ManyToManyField(AttachmentFiles, blank=True)
....
Within view I’ve done something like this below
attachments = data.pop("attachments")
if attachments and attachments[0] != "":
for attachment in attachments:
file_data = {"file": attachment}
file_serializer = AttachmentFilesSerializer(data=file_data)
file_serializer.is_valid(raise_exception=True)
file_obj = file_serializer.save()
abc_obj.attachments.add(file_obj)
abc_obj.save()
also I’ve done this for file but as you need this for text or char fields you can modify this accordingly and can implement other logic within this.
Did this code reference helped in your code logic building.