0

I have 2 models named: PaymentMode and Store as:

PaymentMode Model: (id will be auto generated by django)

id = model.IntegerField(primarykey)
name = model.CharField("eg: cash, debit, credit")

Store Model: (id will be auto generated by django)

id = model.IntegerField(primarykey)
payment_mode = model.ForeignKey(Paymentmode, on_delete=model.cascade)
mode_credit_value = model.IntegerField(default=0)
name = model.CharField()
... some other fields

So what i want is whenever a store is created, all payment modes inherits automatically, inherit all foreign keys, this will make multiple rows in store table as: (if store with id 1 is added, 3 rows for 1, similarly for other)

store_id | payment_mode | name
------------------------------
1             1           abcd
1             2           abcd
1             3           abcd
... similarly if store id 2 is passed, all master are inherited
2             1           xyz
2             2           xyz
2             3           xyz

i looked at various stackoverflow question, probably this might be duplicate, but those all overrides save function and make only one inherited object like:

Stackoverflow question

so the question is will save function override approach in above question inherit all foreign keys? or will it make only one?
Obviously payment modes will be created first. Also in future if a payment mode is added, is it possible to add new payment mode to all pre-existing stores automatically?
Probably my approach of creating model is wrong.

Aashish Gahlawat
  • 409
  • 1
  • 7
  • 25

2 Answers2

1

Since this is many-to-many relationship (One store can have many payment modes and one payment mode can be attached to multiple stores). So you should use ManyToMany field.

So the store model will be like:

class Store(models.Model):
      payment_modes = models.ManytoManyField(PaymentMode)

UPDATE:

To add all the payment modes in the Store model, you can override the save method.

def save(self, *args, **kwargs):
    instance = super().save(*args, **kwargs)
    instance.payment_modes.add(*PaymentMode.objects.all())
    return instance

Now to save the payment modes in the store, you can either ask the user what are the available payment modes for the given store or if you know that all payment modes will be supported, then while saving the store fetch all the payment models and assign it to store.

The above approach will have one drawback that if any new payment mode is added then you have to run a script to add to all the previous stores.

Ritesh Agrawal
  • 791
  • 4
  • 7
0

You can define one more model

class AllPayemntModes(models.Model):
    modes = models.ManyToManyField(PaymentMode) 

then add all payment modes to Store model and

class Store(models.Model):
    payment_modes = models.ForeignKey(AllPayementModes)

then whenever a new store is added you only have to add it to the AllPayemntModes the it will be available to all stores. specific_mode = store.payment_modes.modes.get(name=("cashcash/debi/credit) will give you a specific mode.

doubleo46
  • 475
  • 4
  • 12
  • can i directly use many-to-many on store itself as payment_modes = model.ManyToMany(PaymentMode) ? – Aashish Gahlawat Jul 20 '18 at 05:40
  • @Aashish Gahlawat you can, the issue is that whenever you create a store you need to add all those methods to that field and whenever a new payment method is created you need go back and add that to all the stores again – doubleo46 Jul 20 '18 at 07:24