0

Recently I have been developing an app using Django ninja. I want to add Group to user groups, and statement seems have no effect. It does not commit to db. I tried atomic, and all solutions but it does not work. I have db for tests:

    settings.DATABASES["default"] = {
    "ENGINE": "django.db.backends.sqlite3",
    "NAME": ":memory:",
    "ATOMIC_REQUESTS": False,
    "TIME_ZONE": "America/Chicago",
    "CONN_HEALTH_CHECKS": True,
    "CONN_MAX_AGE": 0,
    "OPTIONS": {},
    "AUTOCOMMIT": True
}

This is code:

    group = await cls.repository.get_group_by_name(name)
    assert group

    await user.groups.aadd(group)

In Django documentation this should work just fine, but it does not. I use AsyncClient from Django to run tests.

Any advice ?

3
  • So how do you know/check it does not add it? Commented Jul 28, 2024 at 18:36
  • I checked every single query and commit to db. No found. Also test does not show any groups after receiving user.groups.aall() Commented Jul 29, 2024 at 3:53
  • I noticed, when I use PostgreSQL It works as expected, so it seems the error is only with test db. Any advice? Commented Jul 29, 2024 at 9:05

1 Answer 1

0

Ok, it seems I did not remember that Django does not allow to m2m relations during object creation. Simple solution:

    @receiver(post_save, sender=CustomUser, dispatch_uid="custom_user_saved")
async def custom_user_post_save(sender, instance, created, signal, update_fields, **kwargs):

    if created:
        await UserService.configure_new_user(instance)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.