1

1.I have array of products and want to store it in mysql database using django model.I tried with 'ListTextField' in models.py as below,

products = ListTextField(
        base_field=models.CharField(max_length=255),
        size=100,  # Maximum of 100 products in list
)

and I am using serializer to save data.But it is not working.

2.If I want to save data manually from views.py.How should I pass array of products in model object.for example,

obj=ContactDeatils(name='abc',products=['aa','bb'])

This gives me 'invalid syntax error' near products.

please help.

  • 1
    If you get an error, you should post the full message and traceback. – Daniel Roseman Aug 11 '18 at 13:34
  • "I am using serializer to save data" - what do you mean? Also I would recommend against using ListTextField, use JSONField if you can. – Adam Johnson Aug 12 '18 at 09:52
  • @DanielRoseman This is error message , D:\Technologies\Python_Code\django_develops\django_development\kumo_soft\kumo_soft\urls.py", line 22, in from inventory import views File "D:\Technologies\Python_Code\django_develops\django_development\kumo_soft\inventory\views.py", line 146 obj=new ContactDetails(companyName='aa',websiteUrl='aa',desc='cc',phoneNo='12222222',emailId='ss',address='aa',contactPersonNm='ff',designation='dd',companyType='fff',products= ['aaa','bbb']) ^ SyntaxError: invalid syntax – Vidyashree Boragalli Aug 13 '18 at 05:09
  • @AdamChainz serializer.py contains : from rest_framework import serializers from .models import ContactDetails class ContactDetailsSerializer(serializers.ModelSerializer): class Meta: model = ContactDetails fields = ('companyName', 'websiteUrl', 'desc','phoneNo','emailId','address','contactPersonNm','designation','companyType','products') and I am using this serliazer in views.py as,serializer = ContactDetailsSerializer(data=request.data) serializer.save() – Vidyashree Boragalli Aug 13 '18 at 05:14
  • It worked with JSONField .Thanks for your suggestion @AdamChainz. – Vidyashree Boragalli Aug 13 '18 at 05:43

0 Answers0