Orderin in model vs query

If I order in the model, for example:

class Objectives(models.Model):
    dashboard=models.ForeignKey(Dashboard, on_delete=models.CASCADE)
    row = models.ForeignKey(Row, on_delete=models.CASCADE)
    objective_num = models.IntegerField()

    class Meta:
       ordering = ['row' , "objective_num"]

this would already sort the query by objective number, so for the query:

            formset_list=[ObjectiveTextFormSet(request.POST,prefix=f'row-{i}',
                        queryset=ObjectiveText.objects.filter(
                        row=i
                        ).order_by('objective__objective_num'))
                        for i in range(1,7)
                        ]

I could leave out:

.order_by('objective__objective_num'))
                        for i in range(1,7)

and get a sorted list of objectives (1-n) in each form.

Also, for:

class ObjectiveText(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    dashboard=models.ForeignKey(Dashboard, on_delete=models.CASCADE)
    objective = models.ForeignKey(Objectives, on_delete=models.CASCADE) 
    row = models.ForeignKey(Row, on_delete=models.CASCADE)
    objective_position = models.IntegerField()
    objective_text = models.CharField(max_length=1000, blank = True)
    timestamp= models.TimeField(auto_now = True)

    class Meta:
       ordering = ['row' , "objective_position"]

    def __str__(self):
        return f" Objective: {self.objective} {self.objective_text} at {self.timestamp}"

since each objective_text has a unique position number from 1 to n, (which are unique to the row but the same 1 - n is used in each row; i.e. for 10 per row they are numbered 1 -10 in each row0 anytime I query it they already are sorted by position and I do not need to order them in a query.

Is there a question here for which you are seeking assistance? Or are you describing something you have done for the benefit of the other members?

There is one thing I’ve noticed here - assuming that this is the same project that you have been working through lately, I’m seeing what appears to be redundant or duplicated data in your models.

If I’m understanding what you’re doing, it seems to me that the row and objective_position fields in ObjectiveText are an unnecessary replication of the same values being stored in Objectives. (Or is there a difference now between these that I’m not aware of?)

It was a question, I was wondering if both accomplish the same thing and if one is preferred and why; as I wasn’t sure from the docs and various internet examples. I guess I should have asked that up front.

objective and objective_position are somewhat different ways of referencing the same thing.

Objective position is an absolute reference, Object10 is always objective 10 whether it is the last one in Row 1 or the first in Row 2. That will allow me to create unique css styles for each objective when it comes time to color them based on performance.

Objective_position is a relative reference to where the objective is in relationship to others in the row, and is or will be used to decide how many times to iterate to create a dashboard row or table header titles.

Rows is a table of row numbers. I wasn’t sure how to filter using just the FK in objectives when filtering ObjectiveText.

When in doubt, you can always examine the sql being created for each to see if there’s a material difference.

Which makes them redundant, and potentially conflicting.

If an objectives location is always identified by its absolute position, I don’t see a value of storing a relative position, especially if you’re creating all the entries up front.

The same way you use it in the order_by in your formset_list construction.

ObjectiveText.objects.filter(objective__row=some_row_number)

All of the objectives are not always displayed, but are placeholders for building the final dashboard.

The challenge is objective_num not a position but an identifier, i.e. in css I can refer to object_x and change its attributes, while its position on the page may not be in the same row or position within that row always. So while objective 1 is always before 2, if objective 1 is not used then objective 2 can be in position 1 or a blank space left in position 1; depending how someone wants to display the information.

However, you have got me thinking about deleting objective and just using position. I was going to use position for the color attribute but have found a simple if loop in the HTML will be more efficient. I may not need it after all.

I think if I store text, color and url(eventually each objective will link to a template with data) in ObjectiveText I can delete objective. I guess I could pass the row and position to identify which objective was selected to know what data to use in the view. I have to think about that vs having a consistent number regardless of position. Maybe I should use the id in ObjectiveText since it matches the one in Objective.

Thanks for getting me to think about improving my models.

I will change the rows FK, thanks.