0

I'm trying to add attribute to relationship.

I have a model Scheduler, Product and User.

There are multiple Scheduler objects in database. Each can have multiple products. And each product has some of those schedulers.

The user can choose which scheduler to use with a product.

I want user to be able to name this scheduler (note that one scheduler can be used by many products by multiple users).

Use case:

User creates a product and choose from allowed schedulers. When they choose the scheduler they can assign some name to this scheduler.

For user1, the scheduler with id=5 has name='My scheduler - everyday'

For user2, the same scheduler (id=5) has name='Everyday schedule'

class Product(models.Model):

    user = models.ForeignKey(User, null=False, blank=False)

    scheduler = models.ForeignKey('Scheduler', null=True, blank=True, related_name='products')

class Scheduler(models.Model):
    weekhours = models.ManyToManyField('WeekHour', related_name='schedulers')
    identificator = models.TextField(null=True,blank=True)

Is it possible to do that in some simple way?

3
  • Why not to add fields name and type to the Scheduler? Then user will choose the type of the Scheduler and also specify the name for it. Commented Dec 12, 2016 at 11:50
  • Because if one user1 specified name for the scheduler, it would affect another users too. Commented Dec 12, 2016 at 11:52
  • Make sure you mark this as Solved Commented Jan 6, 2017 at 10:19

2 Answers 2

1

What you can do, is have another model, that maps Users to Schedulers, and have a name attribute there. That model would have user_id, scheduler_id and scheduler_name as attributes.

Otherwise, you can do that in the Product model, and point to the desired Scheduler indirectly.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that what you are describing here is a many-to-many relationship with extra attributes on the through model.
0

I have suggested something similar to

SCHEDULER_TYPE = (
  (1, _('Type1')),
  (2, _('Type2')),

)

class Scheduler(models.Model):

    weekhours = models.ManyToManyField('WeekHour', related_name='schedulers')

    name = models.CharField(max_length=200, verbose_name=_('scheduler name'))

    type = models.PositiveIntegerField(_('scheduler type'), choices=SCHEDULER_TYPE, default=1)

The type of the scheduler will be saved in SCHEDULER_TYPE and won't be afftected if user will change the name of the Scheduler instance.

Also if you need more data you can separate SchedulerCatergory and add additional category field into Scheduler:

category = models.ForeignKey(SchedulerCategory, blank=True, null=True)

In my undestanding when user chooses the type of Scheduler, he created new Scheduler instance of specific type. This Scheduler instance will be unique and won't be affected by other users. It also means that you can easily save any name inside it.

In your words:

User creates a product and choose from allowed schedulers. When they choose the scheduler they can assign some name to this scheduler.

In my opinon: User create a product and choose not from allowed schedulers instances but from scheduler_types or scheduler_categories. Then new instance of Scheduler is created in which you can save name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.