views:

70

answers:

1

Hello All,

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.

+1  A: 

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)
maersu
probably you should change your model definition. Why you need name=models.ForeignKey(model2)? isn't it a redundant information?
maersu
i need a runtime change in name value.I think i need to do it with jquery only.There is no other way for this.First i will only show the login field and when user select the login field i will do ajax call and populate the name field accoriding to it only.
ha22109