0

I'm working in Django 2.0. I have multiple models which share a common field, and am trying to find the related objects. Schematically, given models Model1 and Model2, for each Model1 in a QuerySet, we want to find the corresponding Model2 object such that

Model1.property == Model2.property. 

In this application, there will be at most one, but possibly no, Model2s that meet this condition. This QuerySet will be used in a Django template for displaying results.

Now, I have several ideas as to how to solve this, but I was wondering if there was a standard solution.

Consideration: I know that Django template language does not have a builtin way to find an object with a particular property value

Ideas:

1) For loop in template. Iterate through the Model2s, evaluating Model2.property and testing Model2.property == Model1.property. Storing the Model2 if True. Seems very clunky, and against the philosophy of the Django template language.

2) Deal with it in python. Perhaps attach a new property to Model1

Model1.property.model2 = Model2.objects.filter(property=property)

or use a try/expect block with "get"

3) Deal with it in the query. Perhaps Annotate the results of retrieving the queryset for Model1 with the related Model2s

4) Maybe a custom template tag to add this functionality into the Django template?

Here are my models, for reference:

class ModuleSubscription(models.Model):
subscriber = models.ForeignKey(User, on_delete=models.CASCADE)  # owner of module
module = models.ForeignKey(Module, on_delete=models.CASCADE)  # id of owned module


class ModuleAssignment(models.Model):
delegation = models.ForeignKey(Delegation, on_delete=models.CASCADE)
module = models.ForeignKey(Module, on_delete=models.CASCADE)  # assigned module
access_granted = models.BooleanField(default=False)
module_log = models.ForeignKey(ModuleLog, default=None, on_delete=models.SET_DEFAULT, null=True)

Now, I need the filter ModuleSubscription by subscriber and ModuleAssignment by delegation, and then find the places where the resulting ModuleSubscription.module == ModuleAssignment.module . The reason is that, for each ModuleSubscription, I need the access_granted property from the corresponding ModuleAssignment, if it exists. Then, in the template, I need to loop throughall of the filtered ModuleSubscriptions and display the associated ModuleAssignment.access_granted

4
  • why don't you post your models code; because if they are related you should be using the ForeignKey or other related fields in your models, which makes finding related items quick! Commented Aug 12, 2018 at 19:01
  • Added models to original post Commented Aug 12, 2018 at 19:54
  • This smells like a bad design and feels like a XY problem. I suggest you to take a sheet of paper, write down on it some sample data and follow your ideas. Apply your assumptions to real data. Realize complexity and possible problems with understanding, debugging, troubleshooting performance. Probably you'll come up with some other model schema and some entity that will look like ModuleGrants (user, module, is_granted). Commented Aug 12, 2018 at 20:16
  • Thank you for the advice. The table that you describe is the ModuleAssignment table. The Delegation table includes the user data, so ModuleAssignment basically is (user, model, is_granted). The problem with just using the ModuleAssignment table, is that you would then need to filter by which objects are in ModuleSubscription. It is almost the same problem in reverse. Commented Aug 12, 2018 at 20:29

1 Answer 1

0

(in addition to my comment) I was talking about the table where all the grants would be exposed. Probably I misunderstood what delegation is supposed to mean here.

# filtering part
modules = ModuleSubscription.objects.filter(subscriber=_s)
grants = ModuleAssignment.objects.filter(delegation=_d, module__in=modules)

# template part
for g in grants:
  ...

is that it?

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

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.