Good evening fellows and happy workers day. I have this task, that is, filling an intermediate model with two foreign keys from two other models.
Class A:
string_attr = models.CharField()
string_attr_2 = models.CharField()
Class B:
string_attr_x = models.CharField()
string_attr_y = models.CharField()
Class C:
date_attr = models.DateField()
a = models.ForeignKey(A)
b = models.ForeignKey(B)
The task is given a object from Model A, create as many objects that the table from class B has on model C. That is, given that class B has 4 objects and with the object X from class A, create 4 objects on table C.
Class A is a Pacients Model and class B is a Vital Signs and class C is PacientVitalSign model, for any given Pacient, I want to show a Formset or anything like it, with every vital sign to set on the PacientVitalSign model.
I don't know if the point is clear enough, English is not my mother language. I've look django-extra-views to achieve this task but I haven't hit the right key.
Any ideas?
Thanks
Related
In a specific Django app, I have a DB Model class A that is considered as the main class and many other models are connected to it weather through one to one relationship (like B) or one to many relationship (like C). So, the direct approach to implement such is:
class A(Model):
b = OneToOneField('B', on_delete=CASCADE)
# other fields
class B(Model):
# some fields
class C(Model):
a = ForeignKey(A, on_delete=CASCADE)
# other fields
Now, when I want to create a new object of A, the sequence will be:
create B object
create A object and link the created B object to it
create C object and link the created A object to it.
This happens on a larger scale with a lot of models linked with model A. But I want to have all the relations seen in the A class so that when I want to create an object of A, I go to create all the related objects first after validating them regardless the relationship, then create new A object and link all those related objects to it. So, I did so:
class A(Model):
b = OneToOneField('B', on_delete=CASCADE)
c = ManyToManyField('C') # as there is no OneToManyField
class B(Model):
# some fields
class C(Model):
# some fields
But this solution seems not good as C should have only one A object.
Is it acceptable to do this or is there another good practice approach to do?
OneToMany is equal to ForeignKey constructor.
class A(Model):
b = OneToOneField('B', on_delete=CASCADE)
c = Foreignkey('C', on_delete=CASCADE)
class B(Model):
# some fields
class C(Model):
# some fields
seems totally fine approach to me.
I'm hoping someone can point me to the most Djangoic way to represent the following general relationship with models, so that Django's existing logic naturally enforces the relationship.
Thing A and Thing B both have one of a number of Types. Thing A can be related to many, one, or no Things Bs and vice-versa (in a symmetric fashion), however Thing A and Thing B can be related if and only if they share the same Type.
My current implementation is to have three models, A, B, and Type, where As and Bs have a foreign key to Type, and A has a m2m with B.
class A(models.Model):
b = models.ForeignKey(B)
typ = models.ManyToManyField(Type)
class B(models.Model):
a = models.ForeignKey(A)
class Type(models.Model):
name = models.CharField()
This lets me do what I want, but doesn't enforce the fact that A can't have a B of another Type. I can use filtering logic in views I control, but where I have less control, like in the Admin, Django lets me map As to Bs of different Types. Is there another way to represent the relationship between As, Bs, and Types in Django?
This would be my approach off the top of my head:
class A(models.Model):
typ = models.ManyToManyField(Type)
#property
def related2B(self)
return list of relationships
def save
check that relationship is still valid if typ field changes
class B(models.Model):
typ = models.ManyToManyField(Type)
#property
def related2A(self)
return list of relationships
def save
check that relationship is still valid if typ field changes
class Type(models.Model):
name = models.CharField()
class Relationship(models.Model):
classA = models.ForeignKey(A)
classB = models.ForeignKey(B)
def save
do check that they share a type before saving
Have you looked at limit_choices_to as a way to control the scope of the M2M?
It can take Q objects for complex queries, but am guessing not F ones (for complex queries including the parent object's current state), but you still might be able to make it deny the save if inappropriate
Suppose there is family, each family member can login to my app, when the user login, I want to show the relation between him and the other family member instead of just the family member name.
the real challenge is the relation is dynamic according to the login user, for example:
A is the father of B
C is the wife of B
B and C have a child D
if D login the relation will be
A = granddad
B = father
C = mother
D = me
but if A log in , the relation will be
A = me
B = son
C = daughter in law
D = grandson
the example is fairly simple but the real world situation is much more complicated, like borther and sister, aunt,uncle etc.
Am using django and this is how my model is defined:
class UserProfile(models.Model):
user = models.OneToOneField(User)
family = models.ManyToManyField(FamilyProfile,related_name='family_members')
realname = models.CharField(max_length=100)
date_of_birth = models.DateField(
'user born date', null=True, blank=True)
is_male = models.BooleanField(default=False) # True male
relation_level = models.IntegerField(null=False,blank=False,default=9999)
is_foreign = models.BooleanField(default=False) # True foreign
is_host = models.BooleanField(default=False) # Ture host,
objects = profileManager()
because the relation ship is dynamic , I can't save it to the database, instead, I use the profileManager to calculate the relation ship and add it to the member: member.relation_name, but this is too complicated for me, right now I'm using a lot of if...else statement and its really long, and really stupid, so please help me with this problem .
If you look at it from a DB point-of-view the correct way to do this is to add an 'id' field to the UserProfile model, and also add another class like:
class Relations(models.Model):
relation_id = models.IntegerField()
relation_name = models.CharField()
which would be filled with the relations (like: (1, "son of"), (2, "father of"), etc..) (you might also need to make one string for male and one for female. That's the least interesting point here.
After you've created this list of Relations you need to assign the relations to users, and therefore we make another model:
class UserToRelations(models.Model):
user_a_id = models.IntegerField()
user_b_id = models.IntegerField()
relation_id = models.IntegerField()
[You might just do that with the keys, I really don't remember django that well]
So now you need to populate the UserToRelations table just each time you add a member to the DB. You calculate the relationships there.
The selecting part is where you see the profit of this. Every time a member logs in, you just need to get all the relevant rows from the UserToRelations model and you just present them.
I have two models:
class A(models.Model):
# fields
class B(models.Model):
a = models.ForeignKey(A)
name = models.CharField(max_length=64)
What I want to do is to get a filtered queryset from A in addition to the related objects from B and append them to the queryset from A, so I would be able to access name field this way: A.B.name
Any idea how to do this?
The problem is that, since the relationship is one-to-many, A doesn't have just one B, but rather a b_set
You could so something like:
for b in a.b_set.all():
b.name
But you can't reference just B because that concept doesn't exist. It would however, if you had used a OneToOneField. Then you could easily do:
a.b.name
Because there's only one B for each A. But, you have to model your object after the actual relationships going on, not how you would prefer the api to work.
I dont understand how I can "import"(I dont know the right terminology, so please dont crucify me on this) a Foreignkey from 1 model class to a another:
e.g.
class1 (models.Model):
variable1 = models.CharField()
variable2 = models.CharField()
class2 (models.Model):
variable3 = models.CharField()
variable4 = class1.variable1 "imported" from class1
So now I have all the data from variable1 from class1 available in class2.
I assume this would be accomplished by a ForeignKey.
If I take the example of the official Django documentation (see below), I dont get my answer because:
Why does it only mention the other model and not the variable I create from it.
This would be for a future model, where I dont know the fields yet. But i know the fields already. Again no variable, just the model.
This would be what I am looking for. But this is "imported" from another app. But with me it is in the same app.
ad 1.
class ForeignKey(othermodel[, **options])ΒΆ
ad 2.
class Car(models.Model):
manufacturer = models.ForeignKey('Manufacturer')
# ...
class Manufacturer(models.Model):
# ...
ad 3.
class Car(models.Model):
manufacturer = models.ForeignKey('production.Manufacturer')
Thanks!
If I understand your question correctly.
You are establishing the relationship to the whole model with a foreign key, not just one field, so you will have access to all the fields, even if you only want one.
variable4 = models.ForeignKey(class1)
now you can say variable4.variable1 or variable4__variable2 in a queryset.
Re point 2 - use variable4 = models.ForeignKey('class1') if class1 not already defined
Re point 3 - no need to add application if model in the current application.