I have interested to convert this data to django rest api . for that how to convert it to django model and serializers.
Hi that is quite a broad question but I would recommend you watch parts 1 and 2 of the mosh django course specifically the sections: building a data model in part 1 and Building RESTful APIs with Django REST Framework in part 2. I would not skip any other sections though, because all the material is relevent.
In short however, you would build model classes and define all the keys and values of your JSON objects as attributes and model fields. You can see all the available model fields here. Then to serialize your models you would need serializer classes and reference the model and fields you wish to serialize
There are two possible approaches you can use
First method is to create model inheritance through the use of onetooneField method
class Geo(models.Model):
lat = models.CharField(max_length=20)
lng = models.CharField(max_length=20)
class Address(models.Model):
street = models.CharField(max_length=100)
suite = models.CharField(max_length=20)
city = models.CharField(max_length=50)
zipcode = models.CharField(max_length=10)
geo = models.OneToOneField(Geo, on_delete=models.CASCADE)
class Company(models.Model):
name = models.CharField(max_length=100)
catchphrase = models.CharField(max_length=200)
bs = models.CharField(max_length=200)
class User(models.Model):
id = models.PositiveIntegerField(primary_key=True)
name = models.CharField(max_length=100)
username = models.CharField(max_length=50)
email = models.EmailField()
address = models.OneToOneField(Address, on_delete=models.CASCADE)
phone = models.CharField(max_length=20)
website = models.URLField()
company = models.OneToOneField(Company, on_delete=models.CASCADE)
Second method is to use JsonField, but this method requires extra work to extract values from key, below is an example
class User(models.Model):
id = models.PositiveIntegerField(primary_key=True)
name = models.CharField(max_length=100)
username = models.CharField(max_length=50)
email = models.EmailField()
address = models.JSONField()
phone = models.CharField(max_length=20)
website = models.URLField()
company = models.JSONField()