Django中models.Model旁边的MyModel类(...,models.Model)的多个参数是什么意思?

时间:2016-02-03 12:30:52

标签: django

models.py

如果django在for file in os.listdir(mydir): if file.endswith(".fasta"): with open(file, 'r') as myfile: data = "".join(line for line in myfile if line[:1]!='>') 中,这是什么意思?

1 个答案:

答案 0 :(得分:0)

实际上,这是组织代码的一种非常方便的方式。 A mixin is a special kind of multiple inheritance

AnoterModelMixin可以包含一组可以在模型上使用的方法;这些都是遗传的:

class Post(AnoterModelMixin, AANotherModelMixin, models.Model):
     name = models.CharField(max_length=150)

AnoterModelMixin可能如下所示:

class AnoterModelMixin(object):
"""Additional methods on the Post model"""

   def get_short_name(self):
   """Returns the first 10 characters of the post name"""
      return self.name[:10]

   def get_longer_name(self):
   """Returns the first 15 characters of the post name"""
      return self.name[:15]

然后您可以这样使用它:

demo_post = Post.objects.create(name='This is a very long post name')
demo_post.get_short_name() # 'This is a '
demo_post.get_longer_name() # 'This is a very '