Django模型序列化只返回外键

时间:2017-04-27 05:10:36

标签: python json django

我试图将我的模型序列化为JSON以传递给JavaScript函数。当我序列化模型时,除了外键之外,它返回一切正常。它们返回JSON中的数字主键。

models.py

class NameManager(models.Manager):
    def get_by_natural_key(self,
                           first_name,
                           middle_name,
                           last_name):
        return self.get(
            first_name=first_name,
            middle_name=middle_name,
            last_name=last_name)


class Name(models.Model):
    """Name model - contains properties for Name"""
    objects = NameManager()

    first_name = models.CharField(max_length=200)
    middle_name = models.CharField(max_length=200)
    last_name = models.CharField(max_length=200)

    def natural_key(self):
        return(self.first_name,
               self.middle_name,
               self.last_name,)

    class Meta:
        unique_together = ((
            'first_name',
            'middle_name',
            'last_name'),)


class SponsorManager(models.Manager):
    def get_by_natural_key(self,
                           sponsor_name,
                           sponsor_email,
                           sponsor_phone,
                           sponsor_address):
        return self.get(
            sponsor_name=sponsor_name,
            sponsor_email=sponsor_email,
            sponsor_phone=sponsor_phone,
            sponsor_address=sponsor_address)


class Sponsor(models.Model):
    """Sponser model - contains properties for Sponsor"""
    objects = SponsorManager()

    sponsor_name = models.ForeignKey(Name)
    sponsor_email = models.EmailField(default='Please enter')
    sponsor_phone = models.IntegerField(default=0000000)
    sponsor_address = models.ForeignKey(Address)

    def natural_key(self):
        return(self.sponsor_name,
               self.sponsor_email,
               self.sponsor_phone,
               self.sponsor_address)


class Show(models.Model):
    """Show model - contains properties for Shows"""
    BOX_CHOICES = (
        ('box', 'BOX'),
        ('gred', 'GRED'),
        ('boxgred', 'BOX/GRED'))
    show_date = models.DateField()
    show_time = models.TimeField(default=datetime.now())
    show_type = models.CharField(
        choices=BOX_CHOICES,
        default='box',
        max_length=8)
    show_box_total = models.IntegerField(default=0)
    show_address = models.ForeignKey(
        Address,
        related_name='show_address')
    show_sponsor = models.ForeignKey(
        Sponsor,
        default=1,
        related_name='show_sponser')

views.py

def dashboard_view(request):
    """render the admin view"""
    get_all_shows = Show.objects.all().order_by('show_date')
    json_shows = serializers.serialize('json', get_all_shows)
    print (json_shows)
    context = {
        'json_shows': json_shows
    }

    return render(
        request,
        'website/dashboard_view.html',
        context)

正如您所看到的,我尝试过使用自然键,但效果相同。我可能会开始接近这一切都是错误的,并且以更正确的方法提出建议会很棒。

1 个答案:

答案 0 :(得分:1)

json_shows = serializers.serialize('json', get_all_shows, use_natural_foreign_keys=True)

documentation所述,您需要在序列化时指定use_natural_foreign_keys=True

  

当指定use_natural_foreign_keys=True时,Django将使用natural_key()方法序列化对定义该方法的类型的对象的任何外键引用。