I'm hoping someone can give me a push in the right direction, at least conceptually. What I'm trying to achieve; a model where:
You have users, workstreams and projects;
Workstream should always be linked to a project (i.e. they exist "in" projects)
Users can be linked to a project without being a workstream;
Users can be in multiple teams and projects
I've read something about Models Managers before, should I use these?
Something else I'm struggling with is that users can/should be linked to projects directly or though a team, will this cause problems?
below some snippets from the models.py file
class User(models.Model):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True
)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
class Workstream(models.Model):
name = models.CharField(max_length=100)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
members = models.ManyToManyField(User) #Like this?
class Project(models.Model):
name = models.CharField(max_length=100)
members = models.ManyToManyField(User) #Like this?
Any help in the right directions will be greatly appreaciated!
It is not advisable to link users directly to a project if it is possible to link teams to the same project. It would be possible for a given user to be two times in a Project, as a individual user or as part of a team, and you would have to search for Users and Users inside Teams when looking for all Users linked to a Project, which is cumbersome. Or if a Workstream represents a task to be done inside a Project, it wouldn't make sense to have a User that is not inside a task to be done.
I don't know the specifics of your project, but maybe each User would join a Team that is already part of a Project. A User could join a Team and be the sole member.
class Workstream(models.Model):
name = models.CharField(max_length=100)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
members = models.ManyToManyField(User)
class Project(models.Model):
name = models.CharField(max_length=100)
An admin could assign a Team to a Project or a user that has a 'project leader' privilege could assign a Team to a Project.
Related
I have two different users, one requesting for repairs (Office) and the inspector/repairman which is (Technician). I was wondering if this is a good idea to make both officeid/techid in customUser like this or there's a better way
this is the values of both office and technician, We have hr/accounting for example then EMP-**
this is my full erd so far I might change somethings based on your recommendations
Edit: I also thinking of adding similar to this, incase I create like IT support requesting aircon maintenance from machine office or something
class User(AbstractUser):
is_student = models.BooleanField('student status', default=False)
is_teacher = models.BooleanField('teacher status', default=False)
How about inheriting user class?
from django.contrib.auth.models import AbstractUser
class Technican(AbstractUser):
techName = models.CharField(max_length=64, verbose_name=_('Name'))
class Office(AbstractUser):
name = models.CharField(max_length=64, verbose_name=_('Name'))
head = models.CharField(max_length=64, verbose_name=_('Head'))
Or if you have custom fields that are mutual for both user types, then create first your own base user class and inherit that to Technican and Office.
I have a Django application where registered users can add, through an input form, details of performances of their music ensemble. This application also has a a section for composers, where they add their own composition. I'm using a custom user model, with profiles linked to user accounts:
class User(AbstractBaseUser):
email = models.EmailField(verbose_name="email", unique=True, max_length=255)
first_name = models.CharField(max_length=30, blank=True, null=True)
[...]
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
[...]
This is my 'composition' model:
class Composition(models.Model):
title = models.CharField(max_length=120) # max_length = required
composer = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE
)
[...]
And this is my 'performance' model. The performance information links to the piece performed (performed):
class Performance(models.Model):
performed = models.ManyToManyField(Composition, blank=True)
[...]
So far, so good. Now, I'd like the performers to be able to add pieces by composers who are not (yet) registered to the website. Let's say that the performer performed a piece by John Lennon and is adding information about that performance. So, the performer will need to be able to add both John Lennon, his composition, and link the two.
The most important bit is: if the ghost of John Lennon tomorrow wants to register to the website, the administrator of the website will need to be able to easily link the compositions already added by the performers to John Lennon's newly created account. Is that possible? I suppose the key to solving this issue is changing composer = models.ForeignKey(settings.AUTH_USER_MODEL... with something else, i.e. using a intermediary model. Any suggestion will be greatly appreciated.
There are different ways to do this, you should choose one based on your taste:
When you know that the actual user is registered, just delete the old, fake user referred in the composition and replace it with the actual user.
Create a new model named something like Artist and change the composer relationship to refer to the Artist model. then, link the Artist model to the actual user with a nullable foreign key.
my problem:
I have three models. il (province), ilce (district) and mahalle (neighborhood).
I filter with smart-select. works smoothly in the entry of information. When I looked at the database, I saw that mahalle (neighborhood) data was recorded. but the mahalle (neighborhood) widget sounds empty.
my models:
class il(models.Model):
adi = models.CharField(max_length=20)
class ilce(models.Model):
ill = models.ForeignKey(il, on_delete=models.CASCADE)
adi = models.CharField(max_length=35)
class mahalle(models.Model):
ilcee = models.ForeignKey(ilce, on_delete=models.CASCADE)
adi = models.CharField(max_length=50)
class User(AbstractUser):
username = None
email = models.EmailField(_('email address'), unique=True)
.......
kurum_il = models.ForeignKey('il', on_delete=models.SET_NULL, null=True, blank=False)
kurum_ilce = ChainedForeignKey('ilce', chained_field="kurum_il", chained_model_field="ill", show_all=False, sort=False, null=True, blank=False)
kurum_mahalle = ChainedForeignKey('mahalle', chained_field="kurum_ilce", chained_model_field='ilcee', show_all=False, sort=False, null=True, blank=False)
Does not appear on the Admin page even though I enter and save neighborhood information
related screenshot
Django-smart-selects looks long unmaintained with last release in 2018.
However there are few issues raised / discussed for it that may relate to your question, one of them #237. As a solution it has this Pull Request with little change to .js file (actually it just comments out one line).
As it is not yet included in official release, you can apply these changes manually and provide new .js file to override package one:
copy chainedfk.js from master branch to your project
place it at 'smart-selects/admin/js/chainedfk.js' path in your directory for static files:
place inside static directory in your app and make this app appear before smart_selects in INSTALLED_APPS
or use same static directory or at another path by your preference but specify path to static directory in STATICFILES_DIRS (more preferred, not depend on order in INSTALLED_APPS)
edit this file, apply changes of this Pull Request and any other changes of your choice.
I'm trying to use the Django REST framework to generate json I'll be able to use later on my website. The thing is, I have Users, who have one-to-many projects, and these projects have one-to-many tasks.
The thing is, I'd like to display a list of my users, then when I access the details of an user, I can see his projects. Then, when I check the details of a project, I can see the tasks of this project. Now, my models are defined like this :
class SimpleUser(AbstractBaseUser):
username = models.TextField(max_length=40, unique=True)
firstname = models.TextField(max_length=40)
lastname = models.TextField(max_length=40)
class Project(models.Model):
user = models.ForeignKey(SimpleUser, null=True)
name = models.TextField(max_length=255)
class Task(models.Model):
project = models.ForeignKey(Project, related_name='task_project')
title = models.TextField(max_length=255)
And when I'm trying to display for example the Projects linked to an User, I have an error no matter what I try (I'm pretty sure this is because what I do is wrong), and the error is "SimpleUser has no attribute Project". Which is logical. But I really don't know how to do this, can someone help me ?
You should use project_set to access SimpleUsers Projects.
user = SimpleUser.objects.get(id=1)
projects = user.project_set.all()
Or define a specific name for the manager with related_name=:
user = models.ForeignKey(SimpleUser, null=True, related_name='projects')
Then you can access users projects via projects:
projects = user.projects.all()
Try specifying explicitly the related_name for the relationship:
class Project(models.Model):
user = models.ForeignKey(SimpleUser, null=True, related_name='projects')
name = models.TextField(max_length=255)
If I had a One-To-Many relationship in Django, like the Django example at http://docs.djangoproject.com/en/dev/topics/db/models/#fields, A musician can have many albums, unique to that musician.
class Musician(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
instrument = models.CharField(max_length=100)
class Album(models.Model):
artist = models.ForeignKey(Musician)
name = models.CharField(max_length=100)
release_date = models.DateField()
num_stars = models.IntegerField()
How would I go about implementing the following:
In Django, have 'Musicians' as a section to manage. When you go to manage musicians, you can edit the musician, or you can go on to the albums and manage the albums only for that musician. When you make a new album, the system automatically creates it for the musician you are on.
At the moment, you would have to manage albums individually from a huge list and choose the musician in the edit screen.
This is too complicated for the django admin as is, so either you explore the customization that the docs offers for you or you might consider not using the django admin at all and just write it as a part of your web app.
Check out http://docs.djangoproject.com/en/dev/ref/contrib/admin/#adminsite-objects.
Admin inlines is one solution - http://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects - this will allow you to edit any Albums associated with the musician you are viewing or add a new one.