Inforian
Inforian

Reputation: 1736

How can a model field get the data from another model field?

I have a model in which I specify certain fields.

class Upload_files(models.Model):
    title           =models.CharField(max_length=100, blank=True , null=True)
    image           =models.ImageField(upload_to='images')
    attributes      =models.ForeignKey(Product_attributes, blank=True , null=True)
    value           =models.CharField(max_length=100, blank=True , null=True)
    price           =models.CharField(max_length=100, blank=True , null=True)

    def __unicode__(self):
            return unicode(self.id)

Now i want to create another table which have only three fields(upload id, attribute id and value) . these fields must take the value from above table i.e., upload_id, attribute which i select above and also the value and is only readable from admin pannel.

I used the following code

class Product_values(models.Model):
    product         =models.ForeignKey(Upload_files, blank=True , null=True)
    attributes      =models.ForeignKey(Product_attributes, blank=True , null=True)
    value           =models.CharField(max_length=100, blank=True , null=True)
    def __unicode__(self):
            return unicode(self.product)

I know does not work as i said but how can i do that,so that value and attribute field takes the value by default as i define it in upload_files model.

EDIT

I simply wants that when i insert the values in upload_files model from admin pannel , a instance of uploaded file is created in another table with upload id , attribute and value which i define earlier

Upvotes: 1

Views: 1094

Answers (1)

schacki
schacki

Reputation: 9533

Use the post_save signal, and write and connect a receiver function that creates the Product_values entry like that:

def create_product_entry(sender, instance, created, **kwargs):
    # enter your code here

post_save.connect(create_product_entry, sender=Upload_files)

Upvotes: 1

Related Questions