Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 launch_tests_ci_like.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ for sub_version in {0..21}; do
SQLALCHEMY_VERSIONS+=($version)
fi
done
DJANGO_VERSIONS=("3.2" "4.0" "4.1" "4.2")
DJANGO_VERSIONS=("3.2" "4.0" "4.1" "4.2" "5.0")

# launch test on all versions only if we test 1 package
if [[ ${#PACKAGES[@]} == 1 ]]; then
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 4.2 on 2024-01-18 16:41

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("app", "0004_discountcart_extendedcart_discount"),
]

operations = [
migrations.AddField(
model_name="customer",
name="blocked_customer",
field=models.ManyToManyField(
blank=True, related_name="block_by_customers", to="app.customer"
),
),
migrations.AlterField(
model_name="customer",
name="avatar",
field=models.BinaryField(blank=True, editable=True, null=True),
),
migrations.AlterField(
model_name="flaskcustomer",
name="avatar",
field=models.BinaryField(blank=True, editable=True, null=True),
),
]
2 changes: 2 additions & 0 deletions src/_example/django/django_demo/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class Customer(AutoUpdatedCreatedAt):
is_vip = models.BooleanField(default=False)
avatar = models.BinaryField(null=True, blank=True, editable=True)

blocked_customer = models.ManyToManyField("self", blank=True, related_name="block_by_users", symmetrical=False)


class Order(AutoUpdatedCreatedAt):
class OrderStatus(models.TextChoices):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,27 +125,25 @@ def _build_one_to_one(relation: OneToOneRel) -> OneToOne:

@staticmethod
def _build_many_to_many(relation: Union[ManyToManyField, ManyToManyRel]) -> ManyToMany:
kwargs: Dict[str, str] = {}
kwargs["foreign_collection"] = relation.target_field.model._meta.db_table
kwargs: Dict[str, str] = {"foreign_collection": relation.target_field.model._meta.db_table}

if isinstance(relation, ManyToManyField):
remote_field = relation.remote_field
elif isinstance(relation, ManyToManyRel): # reverse relation
remote_field = relation.field.remote_field

kwargs["through_collection"] = remote_field.through._meta.db_table

for field in remote_field.through._meta.get_fields():
if field.is_relation is False:
continue
if field.related_model == relation.model:
# origin
kwargs["origin_key"] = field.attname
kwargs["origin_key_target"] = field.target_field.attname
elif field.related_model == relation.target_field.model:
# foreign
kwargs["foreign_key"] = field.attname
kwargs["foreign_key_target"] = field.target_field.attname
kwargs["through_collection"] = relation.remote_field.through._meta.db_table

kwargs["origin_key"] = relation.m2m_column_name()
kwargs["origin_key_target"] = relation.m2m_target_field_name()

kwargs["foreign_key"] = relation.m2m_reverse_name()
kwargs["foreign_key_target"] = relation.m2m_reverse_target_field_name()

elif isinstance(relation, ManyToManyRel):
kwargs["through_collection"] = relation.through._meta.db_table

kwargs["origin_key"] = relation.field.m2m_reverse_name()
kwargs["origin_key_target"] = relation.field.m2m_reverse_target_field_name()

kwargs["foreign_key"] = relation.field.m2m_column_name()
kwargs["foreign_key_target"] = relation.field.m2m_target_field_name()

return ManyToMany(type=FieldType.MANY_TO_MANY, foreign_relation=None, **kwargs)

Expand Down Expand Up @@ -174,7 +172,9 @@ def build(model: Model) -> CollectionSchema:
fields[field.name] = DjangoCollectionFactory._build_one_to_one(field)

elif field.one_to_many is True:
fields[field.name] = DjangoCollectionFactory._build_one_to_many(field)
fields[f"{field.name}_{field.remote_field.name}"] = DjangoCollectionFactory._build_one_to_many(
field
)

elif field.many_to_one is True:
fields[field.name] = DjangoCollectionFactory._build_many_to_one(field)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def test_build_should_handle_one_to_many_relations(self):
person_schema = DjangoCollectionFactory.build(Person)

self.assertEqual(
person_schema["fields"]["books"],
person_schema["fields"]["books_author"],
{
"foreign_collection": "test_app_book",
"origin_key": "author_id",
Expand Down