0

I have a task to create a simple post API to send a visitor messages through a contact form that contain fields like full_name,address,phone etc. But instead of creating a model, I have to use an already existing model which has a Jsonfield. Now what I need to do is to use that jsonfield which will have all the fields like name, address etc.

class Core(models.Model):
    """
    Model that saves the corresponding credentials for the slug.
    """

    slug = models.CharField(max_length=255, null=False)
    example = models.JSONField(null=False, default=dict)

    def __str__(self) -> str:
        return self.slug

If it was done in a regular way by creating a model, it would have been like this.

class Contacts(models.Model):
    full_name = models.CharField(max_length=100,blank=True,default="")
    email = models.EmailField()
    phone = models.CharField(max_length= 16)
    address = models.CharField(max_length=255,blank=True,default="")
    message = RichTextField()

    def __str__(self):
        return self.email

Now, how should I send these fields data in a dictionary without in that JsonField without creating a Contacts model?

Reactoo
  • 916
  • 2
  • 12
  • 40

1 Answers1

0

First of all, this question is not clear. Please ask your question in a clearer way. I hope the way below helps you.

This field keeps data like;

{"full_name":"John Doe", "email": "johndoe@doe.com", "phone":"XXXXXXXXXXX"}

You may create dictionary and assign to field. Also you may create json data like this;

json_example = json.dumps(full_name="John Doe, email="johndoe@doe.com, phone="XXXXXXXXXXX")

and then create a Core object ;

core = Core(example = json_example)
core.save()