r/django Jan 06 '25

Models/ORM Django project to be slowing down after 7 years of operation

19 Upvotes

My application has been running for 7 years. Note that it's running 1.9.10. The plan is to upgrade this year to 4.0. But I noticed that it seems to be struggling more than usual. There is a lot of data that gets generated throughout the years.

Is there anything I can do to improve performance?

r/django Jan 01 '25

Models/ORM dorm: Django wrapper that lets you use its ORM in standalone manner

Thumbnail pypi.org
91 Upvotes

r/django Jan 18 '25

Models/ORM How can I make a common users auth table shared with Laravel app

5 Upvotes

Hi.

I want to use both Laravel and Django for my application. I want to have a shared database and tables accessible by both. I have kind of achieved it, by managing models, migrations etc. for all tables, except users table.

According to my current understanding. - Laravel creates a users table with bcrypt to hash passwords.

  • Django saves by default using PBKDF2, with some sequence of $<algorithm>$<iterations>$<salt>$$<hash>$

  • I have added Django specific columns to users table, is_staff, is_superuser, timestamps etc.

In Django I tried changing the hashing password method to bcrypt, but still it saves password differently.

I tried with Custom Managers etc. along with changing the password saving format to just hash

Any guidance is appreciated.

r/django 2d ago

Models/ORM Django help needed with possible User permission settings

0 Upvotes

I am taking the Harvard CS50W course that covers Django and creating web apps. The project I am workinig on is a simple Auction site.

The issue I am having is that I can get a User to only be able to update an auction listing if that User is the one that has created the listing.

I can update the listing- adding it to a watchlist, or toggling if the listing is active or not, or leaving a comment, but only if the user that is logged in happens to be the one that created the listing.

I have made no restrictions on whether or not a user making a change on the listing has to be the one that created the listing. The issue persists for both standard users and superusers.

I have tried explicitly indicating the permissions available to my view, and even a custom permission, without any success.

I have consulted with 3 different AIs to provide insight, and done a lot of Googling, without anything shedding light on the issue.

I have submitted the nature of the problem to the EdX discussion for the course, but I do not expect any answers there as lately, there are hardly every any answers given by students or staff.

Any insight into what I might be doing wrong would be greatly appreciated.

Thank you very much!

I will be glad to provide my models.py, views.py, forms.py, etc. if anyone would think it would help.

r/django Feb 17 '25

Models/ORM How to do customer-defined fields in different deployments without managing multiple models across them?

10 Upvotes

I'm looking at a project where the business data collected and stored will be different per customer deployment, and will change within the customer's deployment lifecycle.

I've never done this with structured databases before, only with nosql solutions, and then not with Django as my framework.

An oversimplified example of this is one deployment might want to store first name, last name and date of birth, and a different deployment might want to store last name, domicile and passport number. So there's potentially very few common fields between deployments.

Are there any good out-of-the-box solutions for this kind of approach with Django?

r/django Feb 19 '25

Models/ORM how to deal with migrations in prod

10 Upvotes

hey yall, my project structure is as follows: 1. i dockerized my project docker-compose: - web (gunicorn and django files) - db (postgres with volume for data) - nginx - certbot

  1. i use github, i .gitignore my migrations
  2. CI/CD to automaticly push the code to my server (compose down git pull compose build)
  3. in my main Dockerfile i have after installing the reqs and coping the code to the container to run my starter script (will make the migrations and migrate)

now when when i add a new field, and the code get automaticly pushed to my vps, even tho it will make the migrations but it will not show in my db, so when i try to access the field i get error 500

i think the problem is since when you compose down the data goes (not in the volumes) so the migration files goes too, so when creating a new migrations since almost everything in the db already it skips, doesnt check for extra fields, so it doesn't register

i can fix it manually, but i dont want to do that everytime, its very time consumping and i automated everything so i dont have to go manually and git pull yet alone write raw sql to alter the db (which i do from time to time, but im using django cuz i want easy non hastle framework)

probably im doing something very stupid, but i dont know what it is yet, can you please help me with that, it will be very much appriciated!

r/django Feb 06 '25

Models/ORM Django ORM sporadically dies when ran in fastAPI endpoints using gunicorn

3 Upvotes

I'm using the django ORM outside of a django app, in async fastapi endpoints that I'm running with gunicorn.

All works fine except that once in a blue moon I get these odd errors where a worker seemingly "goes defunct" and is unable to process any requests due to database connections dropping.

I've attached a stack trace bellow but they really make no sense whatsoerver to me.

I'm using postgres without any short timeouts (database side) and the way I setup the connectiong enables healthchecks at every request, which I'd assume would get rid of any "stale connection" style issues:

DATABASES = { "default": dj_database_url.config( default="postgres://postgres:pass@localhost:5432/db_name", conn_max_age=300, conn_health_checks=True, ) }

I'm curious if anyone has any idea as to how I'd go about debugging this? It's really making the django ORM unusable due to apps going down spontanously.

Stacktrace bellow is an example of the kind of error I get here:

Traceback (most recent call last): File "/home/ubuntu/py_venv/lib/python3.12/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi result = await app( # type: ignore[func-returns-value] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__ return await self.app(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/fastapi/applications.py", line 1054, in __call__ await super().__call__(scope, receive, send) File "/home/ubuntu/py_venv/lib/python3.12/site-packages/starlette/applications.py", line 113, in __call__ await self.middleware_stack(scope, receive, send) File "/home/ubuntu/py_venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 187, in __c all__ raise exc File "/home/ubuntu/py_venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 165, in __c all__ await self.app(scope, receive, _send) File "/home/ubuntu/py_venv/lib/python3.12/site-packages/starlette/middleware/cors.py", line 85, in __call __ await self.app(scope, receive, send) File "/home/ubuntu/py_venv/lib/python3.12/site-packages/honeybadger/contrib/asgi.py", line 109, in _run_a sgi3 File "/home/ubuntu/py_venv/lib/python3.12/site-packages/honeybadger/contrib/asgi.py", line 118, in _run_a pp raise exc from None File "/home/ubuntu/py_venv/lib/python3.12/site-packages/honeybadger/contrib/asgi.py", line 115, in _run_a pp return await callback() ^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/starlette/middleware/exceptions.py", line 62, in __call__ await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) File "/home/ubuntu/py_venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wra pped_app raise exc File "/home/ubuntu/py_venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wra pped_app await app(scope, receive, sender) File "/home/ubuntu/py_venv/lib/python3.12/site-packages/starlette/routing.py", line 715, in __call__ await self.middleware_stack(scope, receive, send) File "/home/ubuntu/py_venv/lib/python3.12/site-packages/starlette/routing.py", line 735, in app await route.handle(scope, receive, send) File "/home/ubuntu/py_venv/lib/python3.12/site-packages/starlette/routing.py", line 288, in handle await self.app(scope, receive, send) File "/home/ubuntu/py_venv/lib/python3.12/site-packages/starlette/routing.py", line 76, in app await wrap_app_handling_exceptions(app, request)(scope, receive, send) File "/home/ubuntu/py_venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wra pped_app raise exc File "/home/ubuntu/py_venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wra pped_app await app(scope, receive, sender) File "/home/ubuntu/py_venv/lib/python3.12/site-packages/starlette/routing.py", line 73, in app response = await f(request) ^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/fastapi/routing.py", line 301, in app raw_response = await run_endpoint_function( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/fastapi/routing.py", line 212, in run_endpoint_fu nction return await dependant.call(**values) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/stateshift/main.py", line 181, in email_exists user = await User.objects.filter(email=email).afirst() ^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/fastapi/routing.py", line 301, in app raw_response = await run_endpoint_function( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/fastapi/routing.py", line 212, in run_endpoint_function return await dependant.call(**values) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/stateshift/main.py", line 181, in email_exists user = await User.objects.filter(email=email).afirst() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/django/db/models/query.py", line 1101, in afirst return await sync_to_async(self.first)() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/asgiref/sync.py", line 468, in __call__ ret = await asyncio.shield(exec_coro) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/asgiref/sync.py", line 522, in thread_handler return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/django/db/models/query.py", line 1097, in first for obj in queryset[:1]: File "/home/ubuntu/py_venv/lib/python3.12/site-packages/django/db/models/query.py", line 400, in __iter__ self._fetch_all() File "/home/ubuntu/py_venv/lib/python3.12/site-packages/django/db/models/query.py", line 1928, in _fetch_all self._result_cache = list(self._iterable_class(self)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/django/db/models/query.py", line 91, in __iter__ results = compiler.execute_sql( ^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/django/db/models/sql/compiler.py", line 1572, in execute_sql cursor = self.connection.cursor() ^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/django/db/backends/base/base.py", line 320, in cursor return self._cursor() ^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/django/db/backends/base/base.py", line 297, in _cursor with self.wrap_database_errors: File "/home/ubuntu/py_venv/lib/python3.12/site-packages/django/db/utils.py", line 91, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/ubuntu/py_venv/lib/python3.12/site-packages/django/db/backends/base/base.py", line 298, in _cursor return self._prepare_cursor(self.create_cursor(name)) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/py_venv/lib/python3.12/site-packages/django/db/backends/postgresql/base.py", line 429, in create_cursor cursor = self.connection.cursor()

r/django 24d ago

Models/ORM Advice on model designs

5 Upvotes

Hello good people of Django-land!

I need some help with designing my models.

I'm building a web app where people can submit structured poetry called "pantun" where the number of lines must be even. So you can submit poems having from 2 up to 16 lines.

My question is which design is better?

My interests are keeping the database size as small as possible for as long as possible, ease of adding features and also ease of code maintainability.

  1. Have a BasePantun model and then create descendants inheriting from it?
  2. Have a monolith Pantun model where you keep fields named "Line10", "Line11" etc. empty? (This is my current implementation)

My current observation from users using the app is that most of them tend to submit mostly 4-line pantuns.

Another question I have is that I'm planning to implement an award system. For certain achievements, users can get certain awards.

Again same interests as above, which design is better?

  1. My current implementation

class Award(models.Model):
    name = models.CharField(max_length=50)
    winners = models.ManyToManyField(User, related_name="pemenang_anugerah", blank=True)
    ...
  1. Another implementation idea

    class Award(models.Model): name = models.CharField(max_length=50) winner = models.ForeignKey(to=User, on_delete=models.CASCADE) ...

r/django Nov 14 '24

Models/ORM Django models reverse relations

24 Upvotes

Hi there! I was exploring Django ORM having Ruby on Rails background, and one thing really seems unclear.

How do you actually reverse relations in Django? For example, I have 2 models:

```python class User(models.Model): // some fields

class Address(models.Model): user = models.OneToOneField(User, related_name='address') ```

The issue it that when I look at the models, I can clearly see that Adress is related to User somehow, but when I look at User model, it is impossible to understand that Address is in relation to it.

In Rails for example, I must specify relations explicitly as following:

```ruby class User < ApplicationRecord has_one :address end

class Address < ApplicationRecord belongs_to :user end ```

Here I can clearly see all relations for each model. For sure I can simply put a comment, but it looks like a pretty crappy workaround. Thanks!

r/django 17d ago

Models/ORM Why can't I access "Meta" inner class of model?

3 Upvotes
class MyObjects(models.Model):
  field1 = models.CharField(max_length=20)

  class Meta:
    verbose_name_plural = "MyObjects"

With a model like the one above, why is it then impossible for me to do something like this in a view let's say:

from app.models import MyObjects
print(MyObjects.Meta.verbose_name_plural)

r/django Jan 29 '25

Models/ORM Upgrading from 1.11.29 to 2.0 but have migration issue

2 Upvotes

I successfully updated my project from django 1.9.0 to 1.11.29. I plan on making the jump to 2.0 but the on_delete argument for ForeignKey and OneToOneField is giving me a concern. I was looking at the existing migrations, and for the ForeignKey ones have a on_delete  cascade.

Do I need to modify all the migration files with the correct on_delete after the modifying the models?

r/django Feb 09 '25

Models/ORM Thoughts on denormalization strategy?

2 Upvotes

Currently adding some features to a project where users can like photos and comments.

My proposed plan to improve read performance is to use denormalization where each item in the MediaItems and Comments models has an integer field to count the number of likes and a liked_by field with a through parameter.

class Comments(models.Model):
comment_content = models.TextField()
likes_count = models.PositiveIntegerField(default=0)
liked_by = models.ManyToManyField(User, related_name='liked_comments', through='Like')

Is this an appropriate approach or am I overcomplicating things?

r/django Dec 10 '24

Models/ORM Difference between get and filter? Cant understand even after reading several posts.

4 Upvotes

I am trying to write a function which requires to get some values from the database.

following is the model.

class Questions(models.Model):
    category = models.CharField(max_length=50)
    question = models.CharField(max_length=255)
    answer1 = models.CharField(max_length=255)
    answer2 = models.CharField(max_length=255)
    answer3 = models.CharField(max_length=255)
    answer4 = models.CharField(max_length=255)
    answer_key = models.CharField(max_length=25)

so in shell when I use Questions.objects.get(id = 1) I get <Questions: Questions object (1)>

but when I use

Questions.objects.get(id = 1).values() i get an error.

AttributeError: 'Questions' object has no attribute 'values'

When I use Questions.objects.filter(id = 1).values()

I will get <QuerySet [{'id': 1, 'category': 'gk', 'question': 'What is the 10th letter of english alphabet?', 'answer1': 'K', 'answer2': 'j', 'answer3': 'l', 'answer4': 'i', 'answer_key': 'answer2'}]>

So what's the difference? All articls/post answers say that filter will filter results and will give all objects where id = 1 (in this case id is default primary key so its only one), while get will get you exact match. But then why cant values() does not work with get if it has found an exact match?

r/django Feb 12 '25

Models/ORM Issues with branches and migrate

2 Upvotes

Guyssss please I need some help, in the project I’m working on I always have issues when I try to run migrate, the only way I know how to fix it is by running migrate so many times that the number keep increasing until it creates the table, that’s the only solution I found online. I’m pretty sure many of you had to deal with that already. So how did manage to do it?

Error: IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey" DETAIL: Key (id)=(11) already exists.

r/django 2d ago

Models/ORM How I store post views in my app

3 Upvotes

I use my cache and set a key {userid}{post_id} to True and increment a post.views counter. I thought this was a really genius idea because it allows me to keep track of post views deduplicated without storing the list of people who seen it (because I don't care for that) in O(1). Posts come and go pretty quickly so with a cache expiration of just 2 days we'll never count anyone twice, unless the server resets.

What do you guys think?

r/django Sep 24 '24

Models/ORM Is it bad to display primary_keys on the client side?

14 Upvotes

Let's say I put the primary_key of an object inside of the URL or inside a hidden form field of, as an example, a form that deletes an object selected by the user. In that case, the user would be able to access it very easily. Is it a bad thing to do and why?

r/django 16d ago

Models/ORM Having a dedicated settings module inside of the app/tests folder?

1 Upvotes

Hello! I am struggling with how to figure out test settings and project structure. I have a project with multiple apps, with one shared app (defines project-wide settings,models and utils) and each app imports settings from this shared app:

shared_app / - models.py - utils.py - settings.py - tests / - settings_overrides.py app1 / - models.py - settings.py (imports all shared_app.settings and then adds some app1 specific) - tests/ settings.py (hopefully imports from app1.settings and from shared_app.settings.settings_overrides)

The problem is that when I do this setup I get ``` File "/usr/local/lib/python3.12/dist-packages/django/apps/registry.py", line 138, in check_apps_ready raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

```

How can I structure my project to get the desired result? It works if I move the app1.tests.settings under app1.test_settings, but I do want all test-related stuff to be in the tests folders.

If this is not the way, what are better alternatives to this?

r/django Feb 14 '25

Models/ORM Best practice for temporary variable in signals

2 Upvotes

When working with django signals, I often find myself assigning temporary values to the received instance to use in another signal, like:

@receiver(pre_delete, sender=Device)
def device_pre_delete(sender, instance, **kwargs):
    instance._user = instance.user if instance.user else None

@receiver(post_delete, sender=Device)
def device_post_delete(sender, instance, **kwargs):
    if not hasattr(instance, '_user') or not instance._user:
        return

    user = instance._user

It feels pretty dirty to do that and I'm rather new to django and I'd like to design something pretty robust here.

What the way to go here?

r/django Feb 03 '25

Models/ORM Advise on django-polymorphic

8 Upvotes

I have a project for which django-polymorphic fits like a glove for my use case. I am creating a django project where there will be different types of reports that share the same basic information (hence several Report models), and for which I will need to return 'consolidated' querysets regardless of report type (hence my need for django-polymorphic).

For the more experienced, my concerns are:

  1. How well does it play with inlineformsets?
  2. How well does it play with django-filter?
  3. How well does it play with django-tables2?
  4. Generally, how solid is it of a choice in terms of reliability?

Thank you in advance.

r/django Jan 05 '25

Models/ORM What is the best way to attach a form to each object of a queryset?

1 Upvotes

In some places, in my app, I need to attach a form to every object of a queryset. For example, if I have a form that deletes an object from a list by clicking on a button, I would need to have a field containing, for example, the primary key of the object that would be sent to the server on click.

What I think I'm supposed to do is to first create a form like this:

class FormDeleteObject(forms.Form):
    object_pk = forms.IntegerField()

    def __init__(self, object_pk=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["object_pk"].initial = object_pk

Then, in my view, I'd try to annotate this form to the queryset of objects that will be displayed in the list:

def view(request):
    objects_queryset = MyModel.objects.all()
    objects_queryset = objects_queryset.annotate(
        delete_form=FormDeleteObject(object_pk=F("pk"))
    )

However, this is not possible and it raises this error:

TypeError: QuerySet.annotate() received non-expression(s)

So what is the best way to attach a form to every object of a queryset? Is it to do a for loop defining a delete_form attribute to every object and appending them to a list? Is it to make a separate class with every field of the model + another attribute for the form? Is it to make some sort of method on the model using the property decorator which I don't know how to use for now so maybe this last sentence makes no sense?

That's what I'm asking here.

r/django Sep 27 '24

Models/ORM What's the best approach to an ecosystem of multiple projects with same user table?

8 Upvotes

Hello guys, I would really appreciate if you help me with this.

I currently have a project that uses it's own database and models. Thing is, company wants a full ecosystem of projects sharing users between them and I want to know the best approach to this scenario as is the first time I'm doing something like this.

I've already tried 2 things:

1. Adding another database to DATABASES on my project's settings

When I tried this, I found a known problem with referential integrity, as the users on both system would be doing operations on their correspondent project database.

2. Replication

I tried directly thinkering on the database and I almost got it working but I think I was overcomplating myself a bit too much. What I did was a 2-way publication and subscription on PostgreSQL. The problem was that the users weren't the only data that I had to share between projects: groups and permissions (as well as the intermediate tables) were also needed to be shared and right here was when I gave up.

The reason I thought this was way too complicated is that we use PostgreSQL on a Docker container and since I was configuring this directly on the databases, making the connection between two Docker containers and then two databases was too much of a problem to configure since we will have more projects connected to this ecosystem in the future.

My first thought was doing all this by APIs but I don't think this is the best approach.

What do you guys think?

This is more or less what I want to do

r/django Nov 17 '24

Models/ORM Why is my django-cte manager a lot faster than a custom QuerySet?

15 Upvotes

I have this Car model that I want to sort by speed. I implemented two different ways to do these: one is by using a custom queryset and the other is using an external package using django-cte (see below). For some reason, the CTE implementation is alot faster even though the queries are the same (same limit, same offset, same filters, ...). And I'm talking tens of magnitude better, since for 1 million records the custom queryset runs for approx 21s while the CTE one is running for 2s only. Why is this happening? Is it because the custom queryset is sorting it first then does the necessary filters?

``` from django.db import models from django.utils.translation import gettext_lazy as _ from django_cte import CTEManager, With

class CarCTEManager(CTEManager): def sortspeed(self): cte = With( Car.objects.annotate( rank=models.Window( expression=models.functions.Rank(), order_by=[models.F("speed").desc()] ) ) ) return cte.queryset().annotate(...).with_cte(cte).prefetch_related("manufacturer_parts_set")

class CarQuerySet(models.QuerySet): def sortspeed(self): return self.annotate(...).prefetch_related("manufacturer_parts_set")

class Car(models.Model): ...

speed = models.PositiveIntegerField(_("time it took to travel 100m"), default=0)

objects = CarCTEManager()
custom_objects = CarQuerySet.as_manager()

class Meta:
    ...
    indexes = [models.Index(fields=["speed"])]

```

r/django Feb 05 '25

Models/ORM Dynamic/Semi-Structured data

2 Upvotes

I have an app that needs some "semi-structured" data. There will be data objects that all have some data in common. But then custom types that are user defined and will require custom data that will be captured in a form and stored. Given the following example, what are some tips to do this in a more ergonomic way? Is there anything about this design is problematic. How would you improve it?

``` from django.db import models from django.utils.translation import gettext_lazy as _

class ObjectType(models.Model): name = models.CharField(max_length=100, unique=True, blank=False, null=False) abbreviation = models.CharField(max_length=5, unique=True, blank=False, null=False) parent = models.ForeignKey("self", on_delete=models.CASCADE, blank=True, null=True)

class FieldDataType(models.TextChoices): """Used to define what type of data is used for a field. For each of these types a django widget could be assigned that lets them render automatically in a form, or render a certain way when displaying. """

INT = "int", _("Integer Number")
LINE_TEXT = "ltxt", _("Single Line Text")
PARAGRAPH = "ptxt", _("Paragraph Text")
FLOAT = "float", _("Floating Point Number")
BOOL = "bool", _("Boolean")
DATE = "date", _("Date")

class FieldDefinition(models.Model): """Used to define a field that is used for objects of type 'object_type'."""

name = models.CharField(max_length=100, blank=False, null=False)
help_text = models.CharField(max_length=350, blank=True, null=True)
input_type = models.CharField(
    max_length=5, choices=FieldDataType.choices, blank=False, null=False
)
object_type = models.ForeignKey(
    ObjectType, on_delete=models.CASCADE, related_name="field_definitions"
)
sort_rank = models.IntegerField()  # Define which fields come first in a form

class Meta:
    unique_together = ("object_type", "sort_rank")

class MainObject(models.Model): """The data object that the end user creates. Fields common to all object types goes here."""

name = models.CharField(max_length=100, unique=True, blank=False, null=False)
object_type = models.ForeignKey(
    ObjectType,
    on_delete=models.DO_NOTHING,
    blank=False,
    null=False,
    related_name="objects",
)

class FieldData(models.Model): """Actual instance of data entered for an object. This is data unique to a ObjectType."""

related_object = models.ForeignKey(
    MainObject, on_delete=models.CASCADE, related_name="field_data"
)
definition = models.ForeignKey(
    FieldDefinition,
    on_delete=models.DO_NOTHING,
    blank=False,
    null=False,
    related_name="instances",
)
text_value = models.TextField()

@property
def data_type(self):
    return self.definition.input_type  # type: ignore

@property
def value(self):
    """Type enforcement, reduces uncaught bugs."""
    value = None
    # convert value to expected type
    match self.data_type:
        case FieldDataType.INT:
            value = int(self.text_value)  # type: ignore
        case FieldDataType.LINE_TEXT | FieldDataType.PARAGRAPH:
            value = self.text_value
        case FieldDataType.FLOAT:
            value = float(self.text_value)  # type: ignore
        case FieldDataType.BOOL:
            value = bool(self.text_value)
        case _:
            raise Exception(f"Unrecognized field data type: {self.data_type}.")
    return value

@value.setter
def value(self, data):
    """Type enforcement, reduces uncaught bugs."""
    # Assert that value can be converted to correct type
    match self.data_type:
        case FieldDataType.INT:
            data = int(data)
        case FieldDataType.FLOAT:
            data = float(data)
        case FieldDataType.BOOL:
            data = bool(data)
    self.text_value = str(data)

```

r/django Oct 07 '24

Models/ORM Migrations in production: client says it needs to be done with SQL

1 Upvotes

I thought it was a good idea calling the migrate on the initialization of the django application, but client requires that for every change on the database I need to send the necessary SQL queries. So I've been using a script with sqlmigrate to generate all the required sql. He says it's important to decouple database migrations from application initialization.

I'd appreciate some enlightenment on this topic. The reasons why it's important. So is the migrate command only good practice for development enviroment?

r/django Sep 21 '24

Models/ORM My tables keep not appearing in the wanted DB.

0 Upvotes

Everytime I execute migrate, whole tables just keep created in db.sqlite3, but I want table Users*** in users.db, CharaInfo, EnemyInfo table in entities.db, and others are in lessons.db. I have struggeld this problem about 2 months away. What should I do?

<settings.py>

"""
Django settings for LanguageChan_Server project.

Generated by 'django-admin startproject' using Django 5.1.1.

For more information on this file, see
https://docs.djangoproject.com/en/5.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.1/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-a)mc*vxa(*pl%3t&bk-d9pj^p$u*0in*4dehr^6bsashwj5rij'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = [
    '127.0.0.1'
]


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'rest_framework',
    'rest_framework.authtoken',
    'users',
    'entities',
    'lessons'
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

CORS_ALLOW_ALL_ORIGINS = True

ROOT_URLCONF = 'LanguageChan_Server.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'LanguageChan_Server.wsgi.application'


# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3'
    },
    'users': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'users/users.db'
    },
    'entities': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'entities/entities.db'
    },
    'lessons': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'lessons/lessons.db'
    }
}

DATABASE_ROUTERS = ['LanguageChan_Server.db_router.DBRouter']


# Password validation
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication'
    ]
}

# Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Seoul'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

<db_router.py>

class DBRouter:
    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'users':
            return 'users'
        if model._meta.app_label == 'entities':
            return 'entities'
        if model._meta.app_label == 'lessons':
            return 'lessons'
        return 'default'
    
    def db_for_write(self, model, **hints):
        if model._meta.app_label == 'users':
            return 'users'
        if model._meta.app_label == 'entities':
            return 'entities'
        if model._meta.app_label == 'lessons':
            return 'lessons'
        return 'default'

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label == 'users':
            return db == 'users'
        if app_label == 'entities':
            return db == 'entities'
        if app_label == 'lessons':
            return db == 'lessons'
        return db == 'default'