orm

'Category' object is not subscriptable Django

'Category' object is not subscriptable Django Question: I am learning Django. I wrote a simple model and some views method in Django rest framework so that I can modify some particular attributes when needed to all the records that need that. Here is the model: from django.db import models class Category(models.Model): name = models.CharField(max_length=255) isActive …

Total answers: 2

sqlalchemy.exc.InvalidRequestError: When initializing mapper mapped class Event->event, expression 'Tag' failed to locate a name ('Tag')

sqlalchemy.exc.InvalidRequestError: When initializing mapper mapped class Event->event, expression 'Tag' failed to locate a name ('Tag') Question: I am trying to use ORM with SQLAlchemy in Python. My current solution fails and throws an exception right in the moment the ORM is first used. I receive the following exception: sqlalchemy.exc.InvalidRequestError: When initializing mapper mapped class Event->event, …

Total answers: 1

How to dynamically define an SQLModel class

How to dynamically define an SQLModel class Question: Context SQLModel is heavily based on Pydantic. The latter has the create_model function allowing you to create/define a model class at runtime, by passing the field definitions as arbitrary keyword arguments. There seems to be no special version of the create_model function built into SQLModel and there …

Total answers: 1

how to get foreignkey names instead of sql reference number django view

how to get foreignkey names instead of sql reference number django view Question: I’m trying to build a simple rest API in Django. I have a transaction, account and stock table. Each transaction points to 1 account and 1 stock(on seperate tables using foreign keys). I’m trying to have my API gets the stock ticker …

Total answers: 1

Django ORM multiple chained JOIN equivalent and aggregation

Django ORM multiple chained JOIN equivalent and aggregation Question: Given the following Django models (lots shown just as an example, could be more or less nested): class ModelA(models.Model): value = models.IntegerField() class ModelB(models.Model): modelA = models.ForeignKey(ModelA, on_delete=models.CASCADE) value = models.IntegerField() class ModelC(models.Model): modelB = models.ForeignKey(ModelB, on_delete=models.CASCADE) value = models.IntegerField() class ModelD(models.Model): modelC = models.ForeignKey(ModelC, on_delete=models.CASCADE) …

Total answers: 2

fix long running filter task in django views

fix long running filter task in django views Question: input_df = pd.read_excel(uploadedfile) variable_column = code search_type = ‘icontains’ filter = variable_column + ‘__’ + search_type li = [] for i in input_df["KT_DB"]: qset=KT_DB.objects.filter(**{ filter: i}) df = pd.DataFrame.from_records(qset.values()) li.append(df) frame = pd.concat(li, axis=0, ignore_index=True) with BytesIO() as b: # Use the StringIO object as the …

Total answers: 1

SQLAlchemy session.execute() return value CursorResult , return row as dict

SQLAlchemy session.execute() return value CursorResult , return row as dict Question: I am currently using sqlalchemy 1.4.2 I am using session.execute(statement) to fetch rows from my Db table. This is the code I have used below: query = f"SELECT * FROM mytable WHERE entity_guid IN {entity_guids}" results = session.execute(query) for result in results: print(results) final.append({**result}) …

Total answers: 1

How to ORM filter a query with parameter list exactly?

How to ORM filter a query with parameter list exactly? Question: I have a query filter query_1 = Model.objects.filter(param = param) I need to filter this list to another ORM and return true or flase only if all the data in the query_1 is in the second query. Like, query_2 = AnotherModel.objects.filter(field__in=query_1) return True only …

Total answers: 3

Fetch relations from ManyToMany Field using Annotation

Fetch relations from ManyToMany Field using Annotation Question: I have my database here. Where I have 2 users connected to one instance of ChatRoomParticipants with a ManyToManyField. I’m trying to get list of related users from a ManyToMany Relation Field from ChatRoomParticipants where I don’t want to show the currently authenticated user in the list …

Total answers: 3

one-to-one relationships with sqlmodel

one-to-one relationships with sqlmodel Question: After working through the tutorial of SQLModel, I don’t remember seeing anything on how to implement 1:1 relationships using Relationship attributes. I found documentation for SQLAlchemy, but it’s not immediately clear how this applies to SQLModel. Code example: How to enforce that User and ICloudAccount have a 1:1 relationship? class …

Total answers: 2