I am trying to build an app but have a hard time to figure out my model. I have already setup the User authentication. Next:
Each User needs to be member of a Company (company has address info etc)
Each User should have a Profile
Invoices should be presented based on Company membership so every member gets the same page of invoices.
So the tables (models) are:
Profile
Invoice
Company
User (this already exists and is the default django User)
What would be the appropiate model relationships between those models. I would really appreciate the written model relationships like ForeignKey and ManyToMany e.d.
We may define relations to Company and User in Profile:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
company = models.ForeignKey(Company, on_delete=models.CASCADE)
# Other fields here
class Invoice(models.Model):
company = models.ForeignKey(Company, on_delete=models.CASCADE)
# Other fields here
You may want to have ManyToManyField as the relation to company based on your domain constraints.
Related
I have extended my user model with the OneToOne model and would now like to be able to change additional information via the Wagtail user editing interface.
Customers App
#customer.model
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='customers')
org_title = models.CharField(max_length=500, default='', blank=True)
I have already seen this topic in wagtail docs: https://docs.wagtail.io/en/v2.9.3/advanced_topics/customisation/custom_user_models.html
But AbstractUser models are used. How can this be achieved with related models through the OneToOne relationship?
I am facing a problem while giving permission to admin users in Django that how to restrict users to see only user specific data from same model. I mean that if 2 user can add data to same model, then how to restrict them to access only that data.
in that model you need to specify which user inserted that value. If you have User model, then you can add new field to your model as User which is ForeignKey field.
When you inserted your data with user property, you can easily filter them with user (user.id)
For example if you have Customer model you can filter the value with user's id (in this case it's merchant_id):
customer = Customer.objects.filter(email=email, merchant_id=merchant_id).all()
And our model looks like this:
class Customer(models.Model):
merchant = models.ForeignKey(Merchant, on_delete=models.DO_NOTHING)
name = models.CharField(max_length=128, null=True)
surname = models.CharField(max_length=128, null=True)
email = models.EmailField(max_length=255, null=True)
and you can define a permissions like:
class Task(models.Model):
...
class Meta:
permissions = [
("change_task_status", "Can change the status of tasks"),
("close_task", "Can remove a task by setting its status as closed"),
]
and you can check that via:
user.has_perm('app.close_task')
Hope it helps,
I have two models that have foreign keys to the User table.
class Device(models.Model):
user = models.ForeignKey(User)
class Client(models.Model):
user = models.ForeignKey(User)
So, is it possible to show an Inline form in the Client's admin that shows the Devices that are related to the Client's User?
I have two objects, Company and Account, in different packages.
They have a many-to-many relation through employee, which has an additional field is_admin.
In Company I define the relation, and reading online, it seems that I don't have to redifine the relation in Account (this will result in a circular import).
Retrieving all the Accounts from the CompanySerializer is no problem,
but I need to be able to get all the Companies registered to an account as well.
This is my thinking:
Account Model:
class Account(AbstractBaseUser):
current_jobs = models.ManyToManyField(
Company, through='Employee') // I need to define current_jobs in some way
//,but this results in circular import
Company Model:
class Company(models.Model):
employees = models.ManyToManyField(
settings.AUTH_USER_MODEL, through='Employee')
Employee Model:
class Employee(models.Model):
class Meta:
unique_together = ('user', 'company')
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
company = models.ForeignKey(Company, on_delete=models.CASCADE)
is_admin = models.BooleanField()
The problem is, how will I now define how to get each of the lists in the serializers of the two.
So the Company Serializer, and the Account Serializer...
If i do not define current_jobs, I get an error stating that current_jobs is not defined, which it ofcourse is not.
I don't understand why you think you need to define current_jobs on Account. That is automatically provided for you via the reverse relationship as company_set; if you need it to be current_jobs you can set the related_name attribute.
The site I'm building allows users to create "posts" and has a Twitter-like concept of followed users. For a given user, I'd like to show all of the posts from the users that they follow.
Here are my simplified models:
class User
# a standard django.contrib.auth user model
class UserProfile(models.Model):
# my AUTH_PROFILE_MODULE for django-profiles
user = models.ForeignKey(User, unique=True)
following = models.ManyToManyField('self', symmetrical=False, related_name="followed_by")
class Posts(models.Model):
user = models.ForeignKey(User)
post = models.TextField()
Question: How do you create a queryset of all Post objects from the Users that a given User is following?
I think I've made it more complicated by creating the "follow" relationship on UserProfile, which is not the model with the ForeignKey relationship with Posts.
UPDATE! Here's the answer:
Posts.objects.filter(user__userprofile__in=UserProfile.objects.get(user=your_user_object).following.all())
Posts.objects.filter(user__in=UserProfile.objects.get(user=your_user_object).following)