OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Solution for Django model composite foreign keys

  • Thread starter Thread starter Marcos DaSilva
  • Start date Start date
M

Marcos DaSilva

Guest
I’m working with two models in Django: Order and OrderItems. I want to establish a one-to-one relationship between them using two columns instead of the usual single column.

I’m aware of solutions like SQL Alchemy’s ForeignKeyConstraint and the django-composite-foreignkey package, but I’d prefer not to use ForeignKeyConstraint and the latter isn’t compatible with Django 4.0. Is there an alternative solution available for creating a composite foreign key that references two columns in Django 4.0?

Code:
class Orders(models.Model):
    objects = models.Manager()
    id = models.AutoField(primary_key=True)
    order_id = models.CharField(max_length=255)
    --------> here it should reference with two columns ( id and department_id ) 
orderItem = models.ForeignKey('OrderCustomers', on_delete=models.PROTECT, db_column='order_item_id', to_field='id')
    order_item_id = models..CharField(max_length=255)
    department_id = models.CharField(max_length=255)
    created_at = DateTimeWithoutTZField(auto_now_add=True)
    updated_at = DateTimeWithoutTZField(auto_now=True)

    def __str__(self):
        return self.order_id
<p>I’m working with two models in Django: Order and OrderItems. I want to establish a one-to-one relationship between them using two columns instead of the usual single column.</p>
<p>I’m aware of solutions like SQL Alchemy’s ForeignKeyConstraint and the django-composite-foreignkey package, but I’d prefer not to use ForeignKeyConstraint and the latter isn’t compatible with Django 4.0. Is there an alternative solution available for creating a composite foreign key that references two columns in Django 4.0?</p>
<pre><code>class Orders(models.Model):
objects = models.Manager()
id = models.AutoField(primary_key=True)
order_id = models.CharField(max_length=255)
--------> here it should reference with two columns ( id and department_id )
orderItem = models.ForeignKey('OrderCustomers', on_delete=models.PROTECT, db_column='order_item_id', to_field='id')
order_item_id = models..CharField(max_length=255)
department_id = models.CharField(max_length=255)
created_at = DateTimeWithoutTZField(auto_now_add=True)
updated_at = DateTimeWithoutTZField(auto_now=True)

def __str__(self):
return self.order_id
</code></pre>
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top