Luke
Luke

Reputation: 1844

Django models - referencing more instances of another model

hi have a model Invoice on which i have a subject_id field referencing a subject, and various fields which represents activities. these are integer fields and represents the number of each activities for that invoice. What i want is to create a new model Activity, which contains fields like label, price for that activity. every Invoice instance could contain a different number of activity, and not a fixed number. How can i do that (maybe it's really simple but i can't figure out it now)?

any help? thanks, Luke

Upvotes: 0

Views: 67

Answers (1)

Timmy O'Mahony
Timmy O'Mahony

Reputation: 53999

class Activity(models.Model):
    label = ...
    price = ...

class Invoice(models.Model):
    activities = models.ManyToManyField("Activity")

This assumes that a single activity can be shared between numerous invoices. If you want the situation where a single activity belongs to a single invoice, but an invoice can contain numerous activities:

class Activity(models.Model):
    label = ...
    price = ...
    invoice = models.ForeignKey("Invoice")

class Invoice(models.Model):
    ...

Upvotes: 2

Related Questions