简体   繁体   中英

How do I separate the many to many by individuals in django models?

How do I change the status for each player's payment? I cannot add a status field there as it will change the status for all the users within many to many fields, I want to customize it to have a different status for each user.

from django.db import models
from users.models import Profile
# Create your models here.

class Payments(models.Model):
    match = models.CharField(max_length=30)
    amount = models.DecimalField(default = 0, max_digits = 5, decimal_places = 2)
    players = models.ManyToManyField(Profile)
    datespent = models.DateField('Date Spent')

I believe you want this:

from django.db import models
from users.models import Profile


class PlayerPayment(models.Model):
    PENDING = 'pending'
    PAID = 'paid'
    STATUS_CHOICES = (
        (PENDING, 'Pending',)
        (PAID, 'Paid')
    )
    user_profile = models.ForeignKey(Profile)
    payment_data = models.ForeignKey("PaymentData")
    status = models.CharField(choices=STATUS_CHOICES, default=PENDING)


class PaymentData(models.Model):
    match = models.CharField(max_length=30)
    amount = models.DecimalField(default = 0, max_digits = 5, decimal_places = 2)
    players = models.ManyToManyField(Profile, through=PlayerPayment)
    datespent = models.DateField('Date Spent')

I took the liberty to change Payments to PaymentData because your models should be in singular form as they represent a single entity (ie a single row in your table).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM