orm

related_name parameter name conflict in django?

related_name parameter name conflict in django? Question: i have a query regarding "related_name" parameter (used while establishing a relationship between models) suppose i assign the value to related_name= "x" then, it sets the given value to reverse relation manager name ex- parent_model_object.x.all() it sets given name to parent model while filtering objects. ex – child_model.objects.filter(x__name …

Total answers: 1

Can I chain where clauses conditionally?

Can I chain where clauses conditionally? Question: I’m using Peewee as ORM extensively and within my DAO API layer I need to conditionally make a query narrower e.g. query = UserModel.select().where(UserModel.last_name == last_name) if first_name is not None: query = query.where(UserModel.first_name == first_name) # … more conditions then df = pd.DataFrame(query.dicts()) is this the most …

Total answers: 2

How to get Column from Table using Bracket Notation in declarative SQLAlchemy 2.0

How to get Column from Table using Bracket Notation in declarative SQLAlchemy 2.0 Question: I use python SQLAlchemy 2.0 ORM in declarative style to define my tables like such: from sqlalchemy.orm import DeclarativeBase from sqlalchemy import select class Example(DeclarativeBase): __tablename__ = ‘example’ foo = mapped_column(Text, nullable=False) I know that I can access table column in …

Total answers: 1

python – split query results into multiple objects based on one column

python – split query results into multiple objects based on one column Question: my program is querying a sqlite database, and thre result is like this (simplified) in the cursor ready to be fetched. connection = sqlite3.connect(IMAGE_LOG_DB_PATH) connection.isolation_level = None cur = connection.cursor() sql_query = "Select date, name, count(*) as sells from sellers group by …

Total answers: 1

Using `sqlalchemy.orm` with composite primary keys

Using `sqlalchemy.orm` with composite primary keys Question: Using sqlalchemy.orm I am trying to link two tables on a composite key, but keep getting an error. Unfortunately, the official docs provide an example that uses a single primary key (not composite), so I tried to come up with a basic example that reproduces the issue: from …

Total answers: 1

List comprehension in django orm

List comprehension in django orm Question: I have Gallery model: class Gallery(models.Model): TERMS_CHOICES = [ (1, ‘1 month’), (3, ‘3 months’), (6, ‘6 months’), (12, ’12 months’), ] date = models.DateField(default=datetime.now().strftime(‘%Y-%m-%d’), editable=False) term = models.IntegerField(choices=TERMS_CHOICES, default=1) I’m trying to create a scheduler function: it have to check expired terms, and delete the gallery. If term …

Total answers: 1

django can i have any more good orm?

Django can I have any more good orm? Question: This related to this previous question: Django models and orm and foreign key problems Here are some changes in the MemosInProduct model: class MemosInProduct(models.Model): product_key=models.ForeignKey(ProductInOrder, on_delete=models.CASCADE, related_name="product_key") memo=models.CharField(max_length=100) _is_deleted=models.BooleanField(default=False) blahblah some codes… I added _is_deleted field is need for soft delete functions. Except MemosInProduct, any model …

Total answers: 1

Django models and orm and foreign key problems

Django models and orm and foreign key problems Question: i want got all of related model’s data, but i got only some of related data. to avoid N+1 problem, i used select_related() and prefetch_related() methods. At first, i have these models: class OrderList(models.Model): order_id=models.CharField(max_length=100) order_status=models.CharField(max_length=100) class ProductInOrder(models.Model): order_key=models.ForeignKey(OrderList, on_delete=models.CASCADE, related_name="order_key") product_id=models.CharField(max_length=100) product_price=models.CharField(max_length=100) class MemosInProduct(models.Model): product_key=models.ForeignKey(ProductInOrder, …

Total answers: 2

Django ORM JOIN of models that are related through JSONField

Django ORM JOIN of models that are related through JSONField Question: If I have 2 models related through ForeignKey I can easily get a queryset with both models joined using select_related class Foo(Model): data = IntegerField() bar = ForeignKey(‘Bar’, on_delete=CASCADE) class Bar(Model): data = IntegerField() foos_with_joined_bar = Foo.objects.select_related(‘bar’) for foo in foos_with_joined_bar: print(foo.data, foo.bar.data) # …

Total answers: 3

django Count aggregate is not working as I intended

django Count aggregate is not working as I intended Question: It’s a code that reproduces the problem I experienced. models.py from django.db import models class User(models.Model): pass class Group(models.Model): users = models.ManyToManyField( User, related_name=’groups’, ) class Notice(models.Model): groups = models.ManyToManyField( Group, related_name=’notices’, ) tests.py from django.test import TestCase from tests.models import User, Group, Notice from …

Total answers: 1