Mdjon26
Mdjon26

Reputation: 2255

one-to-many field in Django

I've got this class:

class PurchaseOrder(models.Model):
    product = models.CharField(max_length=256)
    dollar_amount = models.FloatField()
    item_number = models.AutoField(primary_key=True)

I'm trying to make it so that 'product' has a one to many field. In other words, whenever I am adding a new item in django's default admin page. I want to be able to have the option of putting multiple 'product' for the same dollar amount and item number.

In response to Hedde van der Heide's comment. Would this be how you implement this?

class PurchaseOrder(models.Model):
    product = models.ManyToManyField(Order)
    dollar_amount = models.FloatField()
    item_number = models.AutoField(primary_key=True)

class Order(models.Model):
    order_product = models.CharField(max_length =256)
    def __unicode__(self):
         return self.order_product

Upvotes: 0

Views: 953

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599590

No, your edit is incorrect. That would imply a purchase order could belong to many orders and vice versa, which makes no sense. You want a simple ForeignKey from PurchaseOrder to Order.

Upvotes: 1

Related Questions