Django many-to-one relation with 3 tables

Pefori

I dont want any foreign keys directly in my users table, and by default, when I add a foreing key field in my custom User model, Django generate 2 tabels like this:

enter image description here

When I add a many-to-many field in my Company model I get the 3 desired tables but it's made possible for the same user be in two different companys.

class Company(models.Model):

    class Meta:
        verbose_name = 'Company'
        verbose_name_plural = 'Companys'
        ordering = ['name']
        db_table = 'companys'

    id      = models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, unique=True, verbose_name='ID Empresa')
    name    = models.CharField(max_length=100, verbose_name='Nome')
    users   = models.ManyToManyField(User, related_name='company', verbose_name='Users')

    def __str__(self):
        return self.name

I want Django to generate an additional table with only the Foreing Keys of the two models but keep the behevior of a many-to-one relationship between the two. like this:

enter image description here

Amrez

you can make your third table by using a separate model

class Company(models.Model):
    
    class Meta:
        verbose_name = 'Company'
        verbose_name_plural = 'Companys'
        ordering = ['name']
        db_table = 'companys'
    
    id      = models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, unique=True, verbose_name='ID Empresa')
    name    = models.CharField(max_length=100, verbose_name='Nome')
    
    def __str__(self):
        return self.name
class UserCompanyRelationModel(models.Model):
    
    class Meta:
        db_table = 'usr_comp'
    
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    company = models.ForeignKey(Company, on_delete=models.CASCADE)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Select from multiple tables - One to Many relation

Create tables with Many-to-One relation in Postgresql

Entity Framework 6 Code first One-to-Many relation in 3rd level child tables

Django, haystack, elastic search and one to many relation

Forming Django queryset for one-to-many relation

Many to One Relation in Django With Existing Objects

One-To-Many relation with a specific order in Django

Django - How to create many to one relation

Make one-to-one relation to many tables in EF

How to display 2 SQLite tables with one to many relation into one QTableWidget

3 tables joins on One to many

Many to one relation beetween 3 entities

symfony relation from one entity to many entities from differents tables

select single row of multiple records in One to Many relation tables

Eclipse 'generate entities from tables' and 'many to one' relation

Delete from 2 tables one-to-many relation

Syntax error when making psql Tables one-to-many relation

Django ORM join many to many relation in one query

Django model one foreign key to many tables

Django, assign many-to-many relation between two tables with predefined values in both tables

Merge a one-to-many and a many-to-many relation when joining tables

How to define one-to-many together with many-to-many relation in Eloquent model for three tables in Laravel?

One to Many relation in laravel

Laravel one to many relation

One to many morph relation

How to provide a queryset field for a subclass of a one-to-many relation in Django?

how to serialize two classes have one to many relation in django

Django. Choosing a specific field - relation one-to-many

Graphene Django - Mutation with one to many relation foreign key