I wish to create with a database with following models and constraints.
1) Student with attributes name, roll number
2) Exam with attributes exam_code, exam_subject
3) Option with attributes option_name, and ManyToManyField on Exam
4) Application with user, exam, ManyToManyField on Options(new)
Basically there will be many exams and options. Student is entitled to choose a subset of options pertaining to his examination choice.
edit: With the new model Application, I suppose the problem boils down to using javascript in the interface for limiting the options available in the interface.
The next challenge lies in handling students with multiple subjects and their subset of options should be a intersection of options(as two different exams might have options in common) available for both exams. Any guidance on this part would be great.
As it stands right now, the Student doesn't really play into the equation. All that matters is what options are available for the particular exam in question (where Student is only nominally related in that the particular exam is a data point on it).
So, that being the case, the available options to a student is always a function of:
some_student.exam_taken.available_options.all()
What you may be wanting, is the ability for a student to have taken multiple exams, and then have all of the available options for all of the exams taken -- a sort of aggregate.
If that's the case, first, you would require a M2M relationship established between Student and Exam (instead of the Foreign Key). Then, you could get all available options from all exams via:
Option.objects.filter(exam__student=some_student).distinct()
Related
I'm currently stuck with the best way of assigning tutors to students and then tutors can add lessons for these students and they can both see these lessons, but you can only assign one foreign key to the class "MyLessons". So I'm not sure whether it's worth creating a MyStudents class, storing students inside this (but their user is the same as tutors except is_tutor=false) and then for each student creating a MyLessons class that the tutor can add to.
I think I can make an approach I'm just worried it won't be very efficient or there will be some serious problems later on. Such as the other way was that each lesson would auto take in the tutors email (I set the username to email) and then when displaying the tutors lessons it would go through every lesson and display the lessons with a matching email... problems are though, if I reassign a student to a new tutor I'd like the new tutor to see the lessons but this would mean manually changing each and if there are too many lessons and students the process would get slow.
A Tutor has many students. A student potentially has more than one tutor. So at first glance that's a ManyToMany relational field.
One thing that's at first confusing is that the relationship is symmetric, but is defined on one object and only implicitly on the other. But the implicit and explicit fields both work the same, for manipulating a set of relationships.
class Tutor( models.Model)
students = ManyToManyField( Student, related_name='tutors', ...)
then
student_instance.tutors.add( tutor_instance)
tutor_instance.students.add( student_instance)
both accomplish the same. (In the simplest case. Read the doc for the exceptions).
It may or may not help to know that behind the scenes, Django is manipulating an implicit table whose objects have two fields, a ForeignKey to a student and a ForeignKey to a tutor. It can sometimes be useful to make this table explicit, so you can attach extra information to the relationship such as date created, who created it, etc. Look up the "through" option of ManyToMany if you want to do this.
Further examples here.
After a basic introduction to Python thanks to an edX course and a chat with a friend who told me about Django, I thought I could implement a solution for my laboratory. My goal is to keep track of every reagent order made by everyone of us researchers to the suppliers.
After one month I have a pretty decent version of it which I'm very proud of (I also have to thank a lot of StackOverFlow questions that helped me). Nonetheless, there's one requirement of the ordering flow that I haven't been able to translate to the Django app. Let me explain:
Users have a form to anotate the reagent (one per form) they need, and then it is passed to the corresponding manufacturer for them to send us an invoice. It's convenient that each invoice has several products, but they all have to: a) be sold by the same manufacturer, b) be sent to the same location and c) be charged to the same bank account (it's actually more complicated, but this will suffice for the explanation).
According to that, administrators of the app could process different orders by different users and merge them together as long as they meet the three requirements.
How would you implement this into the app regarding tables and relationships?
What I have now is an Order Model and an Order Form which has different CharFields regarding information of the product (name, reference, etc.), and then the sending direction and the bank account (which are ForeingKeys).
When the administrators (administratives) process the orders, they asign several of them that meet the requirements to an invoice, and then the problem comes: all the data has to be filled repeatedly for each of the orders.
The inmediate solution for this would be to create a Products Model and then each Order instance could have various products as long as they meet the three requirements, but this presents two problems:
1) The products table is gonna be very difficult to populate properly. Users are not gonna be concise about references and important data.
2) We would still have different Orders that could be merged into the same invoice.
I thought maybe I could let the users add fields dynamically to the Order model (adding product 1, product 2, product 3). I read about formsets, but they repeat whole Forms as far as I understood, and I would just need to repeat fields. Anyway, that would solve the first point, but not the second.
Any suggestions?
Thanks, and sorry for the long block!
In my model Person can have more than one partner, so the ManyToMany Model is the only answer (this has to be ManyToMany for other reasons, so please don't think about changing it).
If I use default widget i got a list of all persons with selected one, two maybe three persons (which are partners of Person).
It would be ok, if the Person list is about 10-100 - and it works on test database. But my prod database has 10.000 persons or more, so the ModelMultipleChoice widget would be filled with 10.000 records, and finding the correct partner will last ages, and memory usage isn't trivial.
I need only to see names of partners with possibility to unset it from person, and some possibility to add new just typing name. The best solution is to show listbox with filtered names after typing three or four first letters, but any solution would be great.
With forms it would be easier, but I need it in admin, so I'd like to use django admin standards.
What is the diff between these HasManyThrough vs HasAndBelongsToMany ??
https://docs.strongloop.com/display/public/LB/HasManyThrough+relations
VS
https://docs.strongloop.com/display/public/LB/HasAndBelongsToMany+relations
The answer lies in the relation name itself. I'm going to use examples provided in the documentation.
HasManyThrough:
physician hasMany patients through appointment
patient hasMany physician through appointment
Here, both physicians and patients are related to each other through appointments but are not directly related. appointment model is helping to create a relation as it belongs to both physician and patient. Because of appointment, a patient can book an appointment to as many physicians. And a physician can see as many patients who booked him/her.
HasAndBelongsToMany:
student has many classes and belongs to many classes.
classes has many students and belongs to many student.
Here, both students and classes are related to each other directly. students generally attend many classes like physics, chemistry etc - so they have many classes. And since attendance is taken in each class, therefore students belong to many classes.
Likewise, a class is attended by many students - so class have many students. And since attendance is maintained for each student, therefore class belong to many students.
Note:
if two classes are related with many-to-many relationship through an intervening model like appointment, then use hasManyThrough.
if two classes are directly related with many-to-many relationship, use hasAndBelongsToMany
hasManyThrough is just a hasMany relation for which a through model has been defined. See params.through in https://github.com/strongloop/loopback-datasource-juggler/blob/4c9e91423f3d356bb544790075dcb7f891450096/lib/relation-definition.js#L659.
hasAndBelongsToMany is essentially a shortcut to set a hasManyThrough relation on both sides. If a through model has not been defined it just concatenates both model names and tries to find a model by that name, if that fails it just tries to fetch relations from a table by that name (https://github.com/strongloop/loopback-datasource-juggler/blob/4c9e91423f3d356bb544790075dcb7f891450096/lib/relation-definition.js#L1630).
If you define a hasAndBelongsToMany relation between Assembly and Part, it will set a hasMany and belongsTo relations between Assembly and AssemblyPart and also another pair of them between Part and AssemblyPart. If AssemblyPart model is not defined, it will just try to use a table (or collection I guess if MongoDB, I'm mostly a RDBMS guy) named assemblypart. It will also define a hasManyThrough relation from Assembly to Part using AssemblyPart as the through Model (so that assembly.parts is available) and its counterpart from Part to Assembly (part.assemblies).
What I'm trying to say is that you can achieve exactly the same as hasAndBelongsToMany by manually defining an AssemblyPart model with the corresponding hasMany and belongsTo to both Assembly and Part models and the two hasManyThrough: from Assembly to Part and from Part to Assembly.
Trust me, I've put a lot of thought into this problem before asking here, and I think I got a solution, but I'd like to see what you guys can come up with before settling for my own :)
SCENARIO:
In the domain we got 3 entities: A treatment, a beautyshop and an employee. A beautyshop can hire 0 to many employees. Now, a beautysalon has a list of possible treatments it can do for its costumers. Each treatment has a description, a duration and a price. Each employee has a similar list, but each employee can specialize each treatment (different price or duration), add new treatments or "remove" treatments derived from the beautyshop.
.. This seems like a rather common problem to me, so I was hoping someone could come up with something clever :)
So far, Im thinking about letting each treatment have a unique id, and then let the employee list insert treatments by itself which will have the same id as the one from the shop. These employee treatments will then override the shop ones with the same id..
Thanks in advance
Are We talking about the objective representation of the problem or about the database representation of the problem? If it's the objective representation, than a specialized treatment should just be a subclass of the generic treatment.
With the relational database representation of the problem, things get a bit harder:
beautyshop ---= employee
beautyshop ---= treatment_type
treatment_type ---= treatment
employee =--= treatment
(---= is one to many, =--= is many to many).
But how do We get a list of treatments available in a beautyshop? We don't. Instead We get a list of treatments available from all beautyshop employees. That said, if a beautyshop has 0 employees, it serves no treatments.
You might use null fields in treatment table to indicate that the particular employee serves this treatment with default properties. If the treatment_type's defaults change for particular beautyshop, then all treatments are updated.
I would suggest adding some kind of inheritance/specialization mechanism to the Treatments by adding a parentTreatment reference to the Treatment class. You would have a set of standard Treatments, and each Employee would be able to select and customize them.
The BeautySalons wouldn't explicitly store any Treatments, a transient and volatile getAvailableTreatments() method would iterate over the associated Employees and aggregate the parent Treatments of the Treatments offered by each Employee.
Why do you want to have different treatments with the same ID?
I'd rather set up a "custom treatment" ID.