views:

89

answers:

2

I have two classes

class A(models.Model):
    id=models.IntegerField(primary_key=True)
    name=models.CharField(max_length=200)
    store_id=models.IntegerField()
    type=models.ForeignKey(B)

class B(models.Model):
    id=models.IntegerField(primary_key=True)
    type=models.CharField(max_length=10)

 class C(models.Model):
    id=models.IntegerField(primary_key=True)
    store=models.CharField(max_length=200)

 class D(models.Model):
   id=models.IntegerField(primary_key=True)
   type=models.CharField(max_length=10)

In my class A type is a ForeignKey on B and store_id is a logical foreign key on C or D depending upon the value of type.

In my field set I want to show the value of store depending upon *type*after some calculations. The type tells me about the table i.e C Or D and the vlaue of store tell me the row in that table c or d.Now i only want to show the value on the browser without overwriting the values.Is this possible?

+1  A: 

Maybe you should look at Generic Relations.

zalew
This is only useful if you want to relate to different models. In the OP's case he always relates to `C`, I think.
Felix Kling
@Felix: I still think that @zalew is correct in pointing out that the OP is (apparently) trying to solve an already-solved problem. DRY is not an unbreakable commandment, but it is a good target to strive for. Life is too short and we already have enough wheels. :-)
Peter Rowell
@Peter: I didn't want to say anything against DRY. DRY for the win!! ;) But I think in this case the generic relations thing is too much as `A` is always pointing to `C`. In my understanding, generic relations make sense if a model can relate to different types of **models** e.g. a tag model that can relate to a page model or a post model.
Felix Kling
@Felix: I understood that, but his comment 'store_id is a logical foreignkey on C depending upon the value of type' had this weird, hollow ring to it. When meaningful names are stripped from an example like this it makes it more difficult to see if there is a deeper issue that needs to be addressed. (BTW, I really like DRY FTW!)
Peter Rowell
A: 

If I got you correctly, then store_id always points to Cright? But this value depends on the type field?
If so you can set up a normal relation between those models and set the store_id based on the type field during saveing:

class A(models.Model):
    id=models.IntegerField(primary_key=True)
    name=models.CharField(max_length=200)
    store=models.ForeignKey(C)
    type=models.ForeignKey(B)

    def save(self,*args, **kwargs):
        if self.type == some_type:
            self.store = get_specific_store_here()
        super(A, self).save(*args, **kwargs)

And later you can access the store name by:

# a is an object of model A
a.store.store

If you want to stick with your generic integer field you can just add a custom method:

class A(models.Model):
    id=models.IntegerField(primary_key=True)
    name=models.CharField(max_length=200)
    store_id=models.IntegerField()
    type=models.ForeignKey(B)

    def get_store_name():
        store = C.objects.get(pk=self.store_id)
        return store.store
Felix Kling