0

I am training myself on Django and working on a movie blog and have issues with my fixtures that I scrape from themoviedb's api. I am wondering the right way to insert a ManyToMany with intermediary model. Here is my current state (removing the useless fields):

class Movie(models.Model):
    tmdb_id = models.AutoField(primary_key=True)
    original_title = models.CharField(max_length=255)
    actors = models.ManyToManyField(Person, through='Actor', related_name="movie_cast")
    staffs = models.ManyToManyField(Person, through='Staff', related_name="movie_staff")

class Actor(models.Model):
    person = models.ForeignKey(Person, on_delete=models.CASCADE)
    movie = models.ForeignKey(Movie, on_delete=models.CASCADE)
    role = models.CharField("person's role in a movie", max_length=40)

class Staff(models.Model):
    person = models.ForeignKey(Person, on_delete=models.CASCADE)
    movie = models.ForeignKey(Movie, on_delete=models.CASCADE)
    department = models.CharField(max_length=30)
    job = models.CharField("person's job in a movie", max_length=40)

What I am doing now is insert manualy a model.actor or model.staff like this to the fixture. Edit: this is created in a Django command that make the .json, then inserted whith loaddata.

{"model": "blog.actor",
 "fields": {
     "person": people['id'],
     "movie": movie['id'],
     "role": people['character'],
     }
}

I try to pass this dict or just the additional field to movie.actor field but it needs a int foreign key.

Am I doing right or there is a better way to do this ?

Thanks.

Louis L
  • 1
  • 1
  • what is `people['id']`? A fixture is a JSON file, so it cannot contain any variables or stuff like that. So yes, it needs to be an int. Or use [natural keys](https://docs.djangoproject.com/en/2.2/topics/serialization/#natural-keys) if you can somehow use a different combination of fields that is unique for a person. – dirkgroten Oct 21 '19 at 12:59
  • I forgot to mention that this dict is created in a django Command, creating a json file that I insert in my DB with manage.py loaddata. people['id'] is get from the tmdb's api. – Louis L Oct 21 '19 at 13:36
  • so what's the issue you're having? Does is not work? Is there an error? – dirkgroten Oct 21 '19 at 14:59
  • Yes, it is not realy an issue, I just want to know if am I doing this correctly because the doc don't explain this part of fixtures (maybe because the way to do it is obvious ?) – Louis L Oct 21 '19 at 15:08
  • If it works then this is fine. What part of the docs are you referring to that don't explain fixtures? – dirkgroten Oct 21 '19 at 15:22
  • I am referring to [Providing Initial Data](https://docs.djangoproject.com/en/2.2/howto/initial-data/) and [Serializing Django Object](https://docs.djangoproject.com/en/2.2/topics/serialization/). Serializing expalain to pass the primary key but not how to work with intermediary model. – Louis L Oct 21 '19 at 15:49
  • As you said, your intermediary model is just another model with two fk's, so there's no difference. – dirkgroten Oct 21 '19 at 16:11
  • Note that if the purpose of what you're doing is to regularly update entries in your db with new entries fetched from somewhere else, using fixtures and `loaddata` is typically tedious because it requires the pk of the objects to be specified. I would just use the standard `create()` or `save()` methods of the models instead, as you would in your regular code, letting django create the rows and ids by itself. – dirkgroten Oct 21 '19 at 16:46
  • Thanks for feedback, I do it this way cause it's a school project to learn django and I have to populate the DB using fixtures and there is no need to update entries. – Louis L Oct 21 '19 at 17:02

0 Answers0