0
class Product(models.Model):  

    name = models.CharField(max_length = 127)   
    description = models.TextField()   
    code = models.CharField(max_length = 127)

    def __unicode__(self):
        return self.name


class ProductLot(models.Model):

    product = models.ForeignKey(Product)
    code = models.ForeignKey(Product)
    lot_no = models.CharField(max_length = 30)
    location = models.CharField(max_length = 127)
    incoming = models.IntegerField()
    commited = models.IntegerField()
    available = models.IntegerField()
    reorder = models.IntegerField()
    created_date = models.DateField(auto_now_add=True)

    def __unicode__(self):
        return self.product.name + " - " + self.lot_no

I want the code to correlate with product foreignkey so what you enter in for code correlates to the product.


ok Im trying to get a drop down box for the codes that correspond to a product. For example, when In Django I use the ForeignKey for a dropdown box that uses the products in the database but they also have a corresponding code number that doesn't show up in the code box as a dropdown box. I was thinking an embedded class code? Sorry I'm new to this

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Jon
  • 111
  • 1
  • 3
  • 7
  • What do you exactly mean with 'two foreign keys that relate to one another'? That those two foreign keys always point to the same object? Or how exactly is the relation between those two foreign keys? – Haes Jan 12 '10 at 20:05
  • possible duplicate of [How can I have two foreign keys to the same model in Django?](http://stackoverflow.com/questions/543377/how-can-i-have-two-foreign-keys-to-the-same-model-in-django) – viam0Zah Sep 17 '10 at 18:49

3 Answers3

3

if you have two FK to one model you need to give different related names:

product = models.ForeignKey(Product, related_name='lot_product')
code = models.ForeignKey(Product, related_name='lot_code')

related_name comes from django docs:

ForeignKey.related_name

The name to use for the relation from the related object back to this one.

See the related objects documentation for a full explanation and example.

Community
  • 1
  • 1
Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
1

Make code a property that looks at product:

def getCode(self):
  return self.product and self.product.code

def setCode(self, value):
  if self.product:
    self.product.code = value
    self.product.save()

code = property(getCode, setCode)

You won't be able to use it in a query, but that's what product__code is for.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

I think you should revise your models. Right now, each instance of ProductLot points exactly to two products. I'm not sure this is what you want.

It really depends on what the relation between code and product is. Some thoughts:

  1. Every Product has a unique code (1:1 relation):

    Just omit the second ForeignKey named code in model ProductLot.

  2. Every Product might have multiple different codes but each code only points to exactly one Product (1:n relation):

    I'd add another model for your codes such as:

    class Product(models.Model):
        name = models.CharField(max_length = 127)
        ...
    
    class ProductCode(models.Model):
        product = models.ForeignKey(Product)
        ...
    
    class ProductLot(models.Model):
        product_code = models.ForeignKey(ProductCode)
        ...
        def correspondingProductName(self):
            return self.product_code.product.name
    
Haes
  • 12,891
  • 11
  • 46
  • 50
  • every product does have a unique code but the thing I was trying to do was to have it add in the code number automatically once you pick the product name. Also trying to figure out how to make fields viewable but uneditable and how to preform math functions between 2 fields like available-commited=reorder. Thank you so much – Jon Jan 12 '10 at 21:03