Reputation: 359
ForeignKey or ManyToMany - When to use? I wonder which version is better. InvoiceHistory
model will be stored status changes of Invoice
REJECTED = 1
SUBMITTED = 2
APPROVED = 3
HISTORY_CHOICES = (
REJECTED, "Rejected",
SUBMITTED, "Submitted",
APPROVED, "Approved",
)
class InvoiceHistory(TimeStampedModel)
user = models.ForeignKey(settings.AUTH_USER_MODEL)
action = models.IntegerField(choices=HISTORY_CHOICES)
class Invoice(models.Model):
number = models.CharField(...)
history = models.ManyToManyField(InvoiceHistory)
or version with ForeignKey:
class Invoice(models.Model):
number = models.CharField(...)
class InvoiceHistory(TimeStampedModel)
user = models.ForeignKey(settings.AUTH_USER_MODEL)
action = models.IntegerField(choices=HISTORY_CHOICES)
invoice = models.ForeignKey(Invoice)
Upvotes: 1
Views: 63
Reputation: 77902
It's not a question of "which is better", but of "which one correctly models your domain". In your first example, you have a many to many relationship between Invoice and InvoiceHistory, which means that a same InvoiceHistory instance can belong to many Invoice instances. I don't have your project's full context but I'm not sure it makes sense here...
Upvotes: 1