我有一个基础模型Asset
,其他模型从照片和视频中继承了这些模型。我创建了Asset
作为基类来获取特定用户的所有对象。它的工作正常。但我需要知道资产的模型类名称。我怎么得到它?
模型:
class Asset(models.Model):
user = models.ForeignKey(User, related_name = "user_objects")
likes = models.ManyToManyField(User, through="Like", related_name="Liked_user")
comments = models.ManyToManyField(User, through="Comment", related_name="Commented_user")
timestamp = models.DateTimeField(auto_now = True, auto_now_add= False)
updated = models.DateTimeField(auto_now = False, auto_now_add = True)
class Meta:
ordering = ['-timestamp']
def __unicode__(self):
return self.user.username
class Album(Asset):
title = models.CharField(max_length=200)
description = models.TextField()
def __unicode__(self):
return self.title
class Picture(Asset):
description = models.TextField()
image = models.ImageField(upload_to=get_upload_file_name)
album = models.ForeignKey(Album, null=True, blank=True, default = None)
def __unicode__(self):
return self.description
class ProfilePicture(Picture):
pass
假设,我需要知道资产是Album类还是ProfilePicture类?如何返回资产的类名?
修改
例如:
>>> r.user_objects.all()
[<Asset: Asset>, <Asset: Asset>, <Asset: Asset>, <Asset: Asset>, <Asset: Asset>]
我想要的是获取用户的所有资产,并根据孩子的班级名称区分他们。喜欢 -
>>> r.user_objects.all()
[<Asset: Photo>, <Asset: Video>, <Asset: Photo>, <Asset: ProfilePicture>, <Asset: Video>]
我希望我很清楚。请指导我。谢谢。
答案 0 :(得分:1)
assetInstance.__class__.__name__
应该退回。