开发者

django models objects filter

I have a model 'Test' ,in which i have 2 foreignkey

models.py

class Test(models.Model):
    id =models.Autofield(primary_key=True)
    name=models.ForeignKey(model2)
    login=models.ForeignKey(model1)
    status=models.CharField(max_length=200)



class model1(models.Model):
   id=models.CharField(primary_key=True)
   .
   .

   is_active=models.IntergerField()


 class model2(model.Model):
    id=models.ForeignKey(model1)
     .

     .
     status=model.CharField(max_length=200)

      

When i add object in model 'Test' , if i select certain login then开发者_运维问答 only the objects related to that objects(model2) should be shown in field 'name'.How can i achieve this.THis will be runtime as if i change the login field value the objects in name should also change.


Overwrite the save method of class Test. So you could set name to everithing you want based on login.

class Test(models.Model):
    .
    .
    def save(self, force_insert=False, force_update=False):
        self.name = self.login.model_set()[0] # or what ever you want
        super(Test, self).save(force_insert, force_update)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜