如何获得模型细节以及外键模型

时间:2019-03-15 06:03:13

标签: python-2.7 django-rest-framework

我想获取模型Movie的详细信息以及引用它的外键。该怎么做?我正在所有模型中获取细节,但不是明智的模型。我正在尝试使用postman的api请求方法。请提供一些帮助。

models.py

class Movie(models.Model):
    movie_name=models.CharField(max_length=50)
    banner_name=models.CharField(max_length=50)
    certified=models.CharField(max_length=10)
    artist=models.ForeignKey('Artist',on_delete=models.CASCADE, )
    Song = models.ForeignKey('Song', on_delete=models.CASCADE, )
    singer=models.ForeignKey('Singer',on_delete=models.CASCADE,)
    fightmaster=models.ForeignKey('Fight',on_delete=models.CASCADE,)
    director=models.ForeignKey('Director',on_delete=models.CASCADE,)
    producer=models.ForeignKey('Producer',on_delete=models.CASCADE,)

    objects = models.Manager()

    def __unicode__(self):
        return ' %s ' % self.movie_name

    class Meta:
        ordering = ["movie_name"]
        verbose_name = "Movie"
        verbose_name_plural = "Movies"

views.py

def getmovies(request, id=None):
 if request.method == "GET":
    print (id)
    if id is not None:
        movie = Movie.objects.filter(id=id).values()
    else:
        movie = Movie.objects.all().values()
        movie1=[]


        for mov in movie:
            movie_dict = {}
            movie_dict["movie_name"] = mov["movie_name"]
            movie_dict["banner_name"] = mov["banner_name"]
            movie_dict["certified"] = mov["certified"]
            song1 = Song.objects.filter(id = mov["Song_id"]).values()
            movie_dict["Song_title"] = song1[0]["title"]
            movie_dict["Song_zone"] = song1[0]["zone"]
            artist1 = Artist.objects.filter(id=mov["artist_id"]).values()
            movie_dict["artist_hero_name"] = artist1[0]["hero_name"]
            movie_dict["artist_heroie_name"] = artist1[0]["heroine_name"]
            movie_dict["numberofcrew"] = artist1[0]["numberofcrew"]
            singer1 = Singer.objects.filter(id=mov["singer_id"]).values()
            movie_dict["singer_name"] =singer1 [0]["name"]
            movie_dict["singer_age"] = singer1[0]["age"]
            movie_dict["singerspecial"] = singer1[0]["special"]
            fight1 = Fight.objects.filter(id=mov["fightmaster_id"]).values()
            movie_dict["fight_master_name"] = fight1[0]["master_name"]
            movie_dict["fight_age"] = fight1[0]["age"]
            director1 = Director.objects.filter(id=mov["director_id"]).values()
            movie_dict["director_director_name"] = director1[0]["director_name"]
            movie_dict["director_age"] = director1[0]["age"]
            movie_dict["director_numberofmovies"] = director1[0]["numberofmovies"]
            producer1 = Producer.objects.filter(id=mov["producer_id"]).values()
            movie_dict["producer_producer_name"] = producer1[0]["producer_name"]
            movie1.append(movie_dict)
    return success("success", movie1)

实际结果:

"data": [
        {
        "artist_heroie_name": "Amber Heard",
        "banner_name": "warner bros pictures",
        "movie_name": "AQUAMAN",
        "director_age": 52,
        "fight_master_name": "Mc shell",
        "numberofcrew": 60,
        "singer_name": "THyomas Bergerson",
        "fight_age": 40,
        "singer_age": 48,
        "certified": "u",
        "director_numberofmovies": 15,
        "singerspecial": "fast",
        "director_director_name": "James Wan",
        "artist_hero_name": "Jason Hamora",
        "producer_producer_name": "peter satron"
    },
    ]

但是我希望答案是:我该怎么做?

 "data": [
        {
        ,
        "banner_name": "warner bros pictures",
        "movie_name": "AQUAMAN",
        "director_age": 52,
        {
        "fight_master_name": "Mc shell",
         "fight_age": 40,
        }
        {
        "singer_name": "THyomas Bergerson",
        "singer_age": 48,
        "singerspecial": "fast"
         }
        "certified": "u",
         {
        "director_numberofmovies": 15,
        "director_director_name": "James Wan",
         }
        {
        "artist_heroie_name": "Amber Heard"
        "artist_hero_name": "Jason Hamora",
        "numberofcrew": 60,
         }
         {
        "producer_producer_name": "peter satron"
          }
    },
    ]

0 个答案:

没有答案