[Fixed]-Create JSON fixture for django models

1๐Ÿ‘

I recommend to update your models so that they become related to each other.

For example,to the User model add a many-to-many relationship, possibly with a through table to hold the attributes of such relationship (e.g. when it was rented/watched by the user)

class User(models.Model):
    user_id = models.IntegerField()

    videos = models.ManyToManyField(VideoData, through='VideoRenting', through_fields=('user', 'videodata'))

    user_name = models.CharField(max_length=40)
    email = models.EmailField()
    city = models.CharField(max_length=40)
    class Meta:
        ordering = ['user_id']
        verbose_name = 'User MetaData'
        verbose_name_plural = 'Users MetaData'
    def __unicode__(self):
        return str(self.user_id)

class VideoRenting(models.Model):
    user = models.ForeignKey(User)
    videodata = models.ForeignKey(VideoData)
    rented_at = models.DateTimeField()

See here for more details.

Obviously you can insert the many-to-many relationship in the VideoData class instead of inside the User class.

Create your objects in the DB, then dump them into JSON with django-admin-dumpdata

๐Ÿ‘คPynchia

Leave a comment