Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrate bug where related_name is not working properly when using… #73

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
- name: Test with pytest
run: |
coverage run $(which pytest) --ds=test_settings typedmodels/tests.py
coverage run $(which pytest) --ds=test_settings typedmodels/tests.py --no-migrations
Empty file added second_testapp/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions second_testapp/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.db import models

from typedmodels.models import TypedModel


class BaseReview(TypedModel):
rating = models.IntegerField()
customer = models.ForeignKey("testapp.Employee", on_delete=models.CASCADE, related_name="reviews", null=True)


class OrderReview(BaseReview):
product = models.ForeignKey("testapp.Product", on_delete=models.CASCADE, related_name="reviews", null=True)
order = models.OneToOneField("testapp.Order", on_delete=models.CASCADE, related_name="order_review", null=True)

1 change: 1 addition & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
INSTALLED_APPS = (
"typedmodels",
"django.contrib.contenttypes",
"second_testapp", # purposefully before to test related models with lazy loading
"testapp",
)
MIDDLEWARE_CLASSES = ()
Expand Down
10 changes: 10 additions & 0 deletions testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,13 @@ class Developer(Employee):
class Manager(Employee):
# Adds the _exact_ same field as Developer. Shouldn't error.
name = models.CharField(max_length=255, null=True)


class Product(models.Model):
name = models.CharField(max_length=255, default="Product name")


class Order(models.Model):
external_id = models.CharField(max_length=255, default="123")
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name="orders")
customer = models.ForeignKey("Employee", on_delete=models.CASCADE, related_name="orders")
24 changes: 23 additions & 1 deletion typedmodels/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import pytest

from second_testapp.models import OrderReview

try:
import yaml

Expand All @@ -27,7 +29,7 @@
Fruit,
UniqueIdentifier,
Child2,
Employee,
Employee, Product, Order, Manager,
)


Expand Down Expand Up @@ -444,3 +446,23 @@ class Tester2(Employee):

class Tester3(Employee):
name = models.IntegerField(null=True)


def test_related_name_is_preserved_for_foreign_keys(db):
customer = Manager.objects.create()
product = Product.objects.create(name="test")
order = Order.objects.create(product=product, customer=customer)

order_review = OrderReview.objects.create(
order=order,
product=product,
rating=5,
customer=customer,
)
assert order_review.order == order
assert order_review.product == product

assert order.order_review == order_review
assert product.reviews.first() == order_review
assert customer.reviews.first() == order_review