i have a problem i don't know how to make multiple many-to-many relations through the same model.
This is my DB relations :
Db architecture
and this is my code, i want to know if what I did is right or not ?
class Projects(models.Model):
Project_name = models.CharField(max_length=50)
Poject_key = models.CharField(max_length=50)
CITools = models.ForeignKey(CITools,null=True,on_delete=models.CASCADE)
UsersO = models.ManyToManyField(User,through='OwnerShips') # for user Ownerships
UserF = models.ManyToManyField(User,through='Favorites') # for user Favorites
This is my OwnerSHips class :
class OwnerShips(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE)
project = models.ForeignKey(Projects,on_delete=models.CASCADE)
And this is my Favorites Class:
class Favorites(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE)
project = models.ForeignKey(Projects,on_delete=models.CASCADE)
I don't think you can use foreign keys (ownerships and favorites) in Project before those classes are created.
Actually i found the solution, i used the attribute related_name, so Django to create this two tables automatically
Related
I have these models
class Tree(models.Model):
field = models.TextField()
class TaskProgress(models.Model):
base_task = models.ForeignKey(BaseTask, on_delete=models.CASCADE)
tree = models.ForeignKey(Tree, on_delete=models.CASCADE)
class BaseTask(models.Model):
trees=models.ManyToManyField(Tree, through='TaskProgress')
class TaskType1(BaseTask):
child1_field = models.TextField()
class TaskType2(BaseTask):
child2_field = models.TextField()
how to get all taskprogress when related to TaskType2 ,
TaskProgress.objects.filter(???)
I added extra field on BaseTask class
TASK_TYPE =[('I','Irrigation'),('C','Care'),('A','Assessment'),('O','Other')]
class BaseTask(models.Model):
trees=models.ManyToManyField(Tree, through='TaskProgress')
worker = models.ManyToManyField(User)
task_type = models.CharField(max_length=1,choices=TASK_TYPE,null=True)
And the filter will be like this
TaskProgress.objects.filter(base_task__task = "I")
I do not think what you are asking is possible, if the models are designed like described. The base_task ForeignKey is specifically pointing at a BaseTask. Even though TaskType1 and TaskType2 inherit from BaseTask, they have no relation in the database. They only look similar.
Option 1: Look into Generic Relations in Django. Basically it allows you to have a ForeignKey relation with more than one type of model. I would not recommend it though. Generic relations are a mess if you don't know want you are doing.
Option 2: Rethink your layout. Maybe you can move the relation to the two TaskTypes instead and adress them via related_name.
class TaskProgress(models.Model):
# base_task = models.ForeignKey(BaseTask, on_delete=models.CASCADE)
tree = models.ForeignKey(Tree, on_delete=models.CASCADE)
class TaskType1(BaseTask):
task_progress = models.OneToOneField(TaskProgress, related_name='task_type_1'
child1_field = models.TextField()
class TaskType2(BaseTask):
task_progress = models.OneToOneField(TaskProgress, related_name='task_type_2'
child2_field = models.TextField()
This way you create a one-to-one-relation between the TaskProgress and the TaskType. You should be able to query one or the other by checking whether a relation exists, e.g. all TaskProgress instances with a relation to a TaskType1 instance.
# Query all TaskProgress instances, that have a TaskType1
TaskProgress.objects.filter(task_type_1__isnull=False)
I have these three models :
Person.
Project.
Role.
Using Django's models system, how to represent the fact that a person is participating in a particular project with a specific role ?
general problem : What is the proper way to handle "ternary associations" using Django ?
I would do it using an intermediary model for m2m relationship and add a field there.
Something like this:
class Role(models.Model):
name = models.CharField(max_lenth=32)
class Project(models.Model):
name = models.CharField(max_lenth=32)
class PersonProject(models.Model):
person = models.ForeignKey('.Person')
project = models.ForeignKey(Project)
role = models.ForeignKey(Role)
class Person(models.Model):
projects = models.ManyToManyField(Project, through=PersonProject)
I am working with an existing database that I can not modify and having some trouble trying to deal with presenting forms for modifying the database in Django. The structure in question is as follows and all models are unmanaged.
class Persons(models.Model):
personid = models.BigIntegerField(primary_key=True, db_column='PersonID')
....
class Phones(models.Model):
phoneid = models.BigIntegerField(primary_key=True, db_column='PhoneID')
number = models.CharField(max_length=60, db_column='Number', blank=True)
type = models.CharField(max_length=15, db_column='Type', blank=True)
...
class Personsphones(models.Model):
personphoneid = models.BigIntegerField(primary_key=True, db_column='PersonPhoneID')
personid = models.ForeignKey(Persons, db_column='PersonID')
phoneid = models.ForeignKey(Phones, db_column='PhoneID')
...
I want to create a form to display all of the 'Phones' associated with a particular 'Persons' and in addition be able to modify/add/remove 'Phones' belonging to a 'Persons'. Right now the only thing I can think of is to display the 'Phones' in a modelformset and then if one is added or removed manually set the 'Personsphones' relation. Any ideas on how to best deal with this model setup?
For making changes to your models you may want to use django-south http://south.aeracode.org/docs/
As far as displaying your 'Phone' under your forms.py you may want to set up class meta like so. With this any changes made to models will reflect on change
class Meta:
model = Persons
exclude = ('user')
In models you may want to use Foreignkey fore relationships between phones and Persons. Better seen in action here https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey
class Project(models.Model):
title = models.CharField()
class Job(models.Model):
name = models.CharField()
user = models.ForeignKey(User)
project = models.ForeignKey(Project)
I have many jobs for each project. How do i get a list of all users of all jobs of a project?
I came up with this:
users = set()
for job in project.job_set.all():
users.add(job.user)
Is there an easier way without explicitely looping through every job?
Use Django's join syntax and start from your User model instead:
users = User.objects.filter(job_set__project=project).distinct()
Alternatively, you could use a ManyToMany relation from Project to User through the Job model which is in effect a join model:
class Project(models.Model):
users = models.ManyToManyField(User, through='Job')
...
And then simply do:
project.users.all()
I have 2 models in django, and im also using ModelForm, my question is the second model have a froreignkey of the 1, and i want to have one page when generating the form. It's possible, how to link the two forms in one page.
Class Event(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField()
class Date(models.Model):
id = models.AutoField(primary_key=True)
start = models.DateTimeField()
end = models.DateTimeField()
event = models.ForeignKey("Event")
I also have
class EventForm(ModelForm)
Class Date(ModelForm)
What i want is to create the event in one page in my templates.
Thanks.
If you want to have this on the Django Admin, then you need to use inline models.
If you plan to create your own form (using ModelForms), then you need to use inline formets.