These are chat archives for django/django

2nd
Dec 2017
Anish Shrestha
@annshress
Dec 02 2017 02:32
@matrixbot How would you solve @denizs 's problem, keeping his current code intact?
matrixbot
@matrixbot
Dec 02 2017 02:40
Ben Finney A database model can't “dynamically contain different field” in a relational database.
Ben Finney So the solution with separate subclasses, that Django handles quite well, is probably the best fit.
Ben Finney When querying samples, you're going to have to deal with the fact it's not impossible that a Source is referenced by multiple different Sample sub-types. So your code will have to deal with that possibility: ask explicitly for “the set of this source's samples that are TemperatureSample`, and so on.
Ben Finney That's done by setting a sifferent related name from each specific model. Heed the warning at https://docs.djangoproject.com/en/1.11/topics/db/models/#be-careful-with-related-name-and-related-query-name
matrixbot
@matrixbot
Dec 02 2017 02:46
Ben Finney so, @deniz saner, I appreciate that the current version of your Python code might try to ensure a Source only gets referenced by one Sample sub-type; but if you want to use a relational database, you'll need to allow for possible cases (including “the application had a bug that allowed mutlipel sample types to reference this source”) by coding your application to know what sub-set it's asking for.
Ben Finney Django is quite good at getting the object paradigm and the relational paradigm working together. Using sub-classes for specific sub-types of an abstract base model is pretty seamless.
Ben Finney But it can only guess so far. There's no way to make the ironclad promise that “every Source will never have Sample instances in the database of more than one type”, so Django can't assume that. It will require that each sub-type has a different related_name for an attribute on Source.
Ben Finney So I think you're looking for an optimisation that isn't really an optimisation. The first time your database does realise that possibility – because of a bug, or because your design changes – you will be very glad you don't have to undo that assumption :-)
Deniz Saner
@denizs
Dec 02 2017 12:49
@matrixbot Thanks for the detailed elaboration! :+1: The reason I asked my question was to check out, whether there was an alternative approach to this. But as you suggested, there’s no way around my initial approach of pinning down which type of data sample the source contains and then querying the appropriate table.
Thanks! :)