请参阅 django-rest-framework 下拉列表中的描述?
See description in django-rest-framework dropdown?
如何在 django-rest-framework 下拉列表中看到主题名称?现在它只是说 Subject object
.
这是我的模型:
class Subject(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
owner = models.ForeignKey('auth.User', related_name='subjects')
code = models.CharField(max_length=50)
name = models.CharField(max_length=50)
description = models.CharField(max_length=50)
class Meta:
ordering = ('created',)
def __unicode__(self):
return '%s' % (self.name)
class Grade(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
owner = models.ForeignKey('auth.User', related_name='grades')
code = models.CharField(max_length=3)
requirements = models.CharField(max_length=200)
ranking = models.IntegerField()
subject = models.ForeignKey(Subject, related_name='grades', on_delete=models.CASCADE)
class Meta:
unique_together = ('subject', 'ranking')
ordering = ('ranking',)
def __unicode__(self):
return '%d: %s' % (self.ranking, self.code)
我看到您已经为您的模型定义了 __unicode__
。但也许您正在使用 Python 3,在这种情况下,对于 Django 模型,您需要定义 __str__
而不是 __unicode__
class Subject(models.Model):
...
def __str__(self): # Use this in Python 3
return '%s' % (self.name)
无关,但这个更简洁return:
def __str__(self):
return self.name
如何在 django-rest-framework 下拉列表中看到主题名称?现在它只是说 Subject object
.
这是我的模型:
class Subject(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
owner = models.ForeignKey('auth.User', related_name='subjects')
code = models.CharField(max_length=50)
name = models.CharField(max_length=50)
description = models.CharField(max_length=50)
class Meta:
ordering = ('created',)
def __unicode__(self):
return '%s' % (self.name)
class Grade(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
owner = models.ForeignKey('auth.User', related_name='grades')
code = models.CharField(max_length=3)
requirements = models.CharField(max_length=200)
ranking = models.IntegerField()
subject = models.ForeignKey(Subject, related_name='grades', on_delete=models.CASCADE)
class Meta:
unique_together = ('subject', 'ranking')
ordering = ('ranking',)
def __unicode__(self):
return '%d: %s' % (self.ranking, self.code)
我看到您已经为您的模型定义了 __unicode__
。但也许您正在使用 Python 3,在这种情况下,对于 Django 模型,您需要定义 __str__
而不是 __unicode__
class Subject(models.Model):
...
def __str__(self): # Use this in Python 3
return '%s' % (self.name)
无关,但这个更简洁return:
def __str__(self):
return self.name