M2M relation Django, with custom intermediate table having an extra field - django-models

I have two models like CinicHospital and Doctor with M2M relationship. The custom intermediate table is created because I need an extra field.
class DoctorHospital(models.Model):
clinic = models.ForeignKey(ClinicHospital, on_delete=models.CASCADE)
doctor = models.ForeignKey(Doctor, on_delete=models.CASCADE)
shift = models.CharField(max_length = 10)
Problem in this DoctorHospital model two fields are auto-filled while creating ClinicHospital and Doctor but I'm trouble to fill the shift field.
Could I combine this shift field in doctor form and save it in intermediate table?

Related

How to real time filter foreign key dropdown, based on previous foreign key dropdown selection in the model form?

I have 2 models, Jobs and Çompany. Both are taken as a foreign key to my current model Tracker. In the tracker CreateView form I am selecting the Company from the drropdown(Company model), also the Job Position(Job model).
The Job model also has a company field, which says for which company the job position is open.
Now, what I want is, in my tracker form, as soon as I select a company from the dropdown, I want only the Job openings of that company to be listed in the Job foreign key dropdown.
Let's say there are 3 job openings for Google, Engineer, Support and Developer. Currently my job position dropdown is displaying all the positons in the job model list. I want to filter them based on the company I select.
And the company also I'm chosing from the dropdown beside only, in the same form.
Is there a way, I can filter the dropdown real time?
Like having a dynamic foreign key dropdown which depends on my Company value chosen(dropdown again)?
class Tracker(models.Model):
company_applied = models.ForeignKey(Company, on_delete=models.CASCADE)
position_applied = models.ForeignKey(Jobb, on_delete=models.CASCADE)
class Jobb(models.Model):
position = models.CharField(max_length=100)
company = models.ForeignKey(Company, on_delete=models.CASCADE)
def __str__(self):
return self.position_applied
class Company(models.Model):
company_name = models.CharField(max_length=100)
...
def __str__(self):
return self.company_name
How to achieve it? I searched a lot, I'm not able to find the exact words to search to get the solution.
Also I'm new to Django..

Using primary key from intermediary model as foreign key in django

I have two models with many to many relationship through an intermediary model. Is it possible to use primary key from the intermediary model as foreignkey in another model?
class Teacher(models.Model):
title = models.CharField(max_length=20)
class Course(models.Model):
title = models.CharField(max_length=20)
teachers = models.ManyToManyField(Teacher, through='Teacher_Course')
class Teacher_Course(models.Model):
teacher = models.ForeignKey(Teacher, on_delete=CASCADE)
course = models.ForeignKey(Course, on_delete=CASCADE)
allotedLectures = models.IntegerField()
class Lecture(models.Model):
teacher_course = models.ForeignKey(Teacher_Course)
content = models.CharField(max_length=255)
Is it valid to use ForeignKey relation in the Lecture model in this way?
Since you have created the through table as a model explicitly it will have a PK, which means that other tables can use that as a FK. TL;DR, yes.

How does ForeignKey work in this Django example?

I am having trouble understanding how to use ForeignKey and what exactly it does. Does it link two tables? Or does it link specific columns between each table? Here is a very basic example:
class Seller(models.Model):
name = models.CharField()
email = models.CharField()
zipcode = models.CharField()
class ItemsForSale(models.Model):
item = models.CharField()
description = models.CharField()
zipcode = models.ForeignKey(Seller)
What I'm trying to do is when someone fills out a form to sell an item, I want the zipcode for that item to be automatically set as the sellers zipcode (so the end user won't even have to fill out a zipcode in the form).
Why is it ForeignKey(Seller) instead of ForeignKey(Seller.zipcode)? I guess I don't understand am I linking the entire 'Seller' table? Or just the zipcode column?
Since I don't understand how ForeignKey works, I don't know how to write a view method that sets the zipcode for ItemsForSale. so I really think the key to helping me understand all this would be to see a proper view method that sets the ItemForSale's zipcode to the Sellers zipcode.
In Django a ForeignKey is a relationship between Objects.
In a database a ForeignKey is a field (column) in a table that links to a row of another table.
Visit the django docs and wikipedia for more information about ForeignKeys.
Now to your Django example:
Every single Item (ItemsForSale) has a Seller. (Note: Model names should always be singular.)
So we add a ForeignKey in your ItemsForSale-model that relates to the item's seller:
class ItemsForSale(models.Model):
...
seller = models.ForeignKey(Seller)
Now you can access the item's seller (and of course all of his attributes (including his zipcode) this way:
ItemsForSale.get(item='Foo').seller.zipcode

CakePHP: hasMany with join table

I have two models: Store and Review. Users of my site can leave a review on a store. In my database, I have a join table, reviews_stores that associates a review with a store.
How do I link my models? I assume a Store should haveMany Review, and a Review should belong to a Store, but the join table is causing issues with CakePHP as CakePHP is assuming my reviews table has a column called store_id.
The reason I'm using a join table is because many parts of my site can be reviewed. For example, brands. A new Review record will be created and a record will be inserted into a brands_reviews table to associate the two records.
Any help would be much appreciated.
Why are you not simply using one Review model and a reviews table with a field "foreign_key" and another field "model"? By this you do not need to duplicate tables and inherit or duplicate models. This will also remove the need for a join table.
If you want to continue to use your db design then you'll have to use a HABTM association over the hasMany association. But even in the case you want to keep that jointable, again, you can use the foreign_key/model and simply have one join table and one reviews table.
By the way, your join table review_store does not follow the conventions, it should be reviews_stores. But then it differs to the schema you've used for brands_reviews. ;)
Seems to me it isn't a many-many relationship but a grouped 1-many relationship. Id lose the join tables and simply have an extra table outlining which 'group' the review belongs to. So the review table would have review_id, link_id(the foreign key for the relevant brand or store), review_type_id(foreign key depicting whether the review is for a brand or store and so on). Then the review_type table only needs to have review_type_id, review_type(varchar).
There's no need for all the join tables for each model you can review, simple store the model name itself in a field in the Review table.
Setup your relationship like so:
class Store extends AppModel {
public $hasMany = array(
'Review' => array('conditions' => array('Review.model' => 'Store')
);
}
You can then do this with as many extra models as you like without having to touch the database.

Cakephp related tables show id's instead of values

i think i have all 'baked' all my relationships correctly but my 'related' table shows id's instead of values.
my db has 3 tables. candidates, qualificationlookups, qualifications.
qualificationlookups, links candidates to qualifications using the id of the candidate and the id of the qualification.
in my view view, for 'candidates', i have a 'related qualificationlookups' table. (generated by baking a candidate hasmany qualificationlookups and a qualificationlookups belongsto candidate relationship)
in my edit view, for 'qualificationlookups', i can correctly set up the candidates and qualifications fields as dropdowns so i know 'qualificationlookups's relationships are fine.
So how do i ask cakephp to list the name of the qualification (from 'qualifications' table) in the 'related qualificationlookups' table on a candidate's page?
i must be missing something...
could someone please point me in the right direction?
Thanks, Vauneen
Whenever CakePHP automagically fetches lists from your tables, it uses the id key for the value and the $displayField for the text.
If your table has a name or title field, CakePHP automatically displays it as the display field. So, either rename the field that you want as your display field (say, candidate_name to just name) or set the $displayField variable in your model:
class Candidate extends AppModel {
var $displayField = 'candidate_name';
}
HTH.
If there is no other data being stored in the qualificationlookups table, change the relationship to candidates -> HABTM -> qualifications.
To do this, you first need to drop the qualificationlookups table. Create a new table called candidates_qualifications with two indexes, candidate_id and qualification_id.
In your Candidate and Qualification models, define a HABTM Relationship. You do not need to make a new CandidatesQualification Model, cake will do it on the fly.

Resources