orm

CakePHP 4.1 User entity as authorization identity associated fields

假装没事ソ 提交于 2020-08-10 13:02:01
问题 I have just created a very minimal project in CakePHP 4.1, mostly mimicking the CMS tutorial, and want to implement a fairly straightforward piece of logic. Using the Authorization module I want to allow a user A to be able to view a user B if 1) they are actually the same user ( A = B ) OR 2) if A is an admin. There are two DB tables - users and user_types . users has a foreign key user_type_id to user_types . This relationship is reflected in code as: ##### in UsersTable.php ##### class

CakePHP 4.1 User entity as authorization identity associated fields

ぐ巨炮叔叔 提交于 2020-08-10 13:01:25
问题 I have just created a very minimal project in CakePHP 4.1, mostly mimicking the CMS tutorial, and want to implement a fairly straightforward piece of logic. Using the Authorization module I want to allow a user A to be able to view a user B if 1) they are actually the same user ( A = B ) OR 2) if A is an admin. There are two DB tables - users and user_types . users has a foreign key user_type_id to user_types . This relationship is reflected in code as: ##### in UsersTable.php ##### class

SQLAlchemy ORM not working with composite foreign keys

杀马特。学长 韩版系。学妹 提交于 2020-07-16 06:41:08
问题 I'm trying to build an example with several related models, like the following. We have a model B with a 1:n relation with a model C; then we have a model A with a n:1 relation with B and a n:1 relation with C. (C has a 2-columns primary key) I tried this code: class C(db.Model): __tablename__ = 'C' key1 = Column(Integer, primary_key=True) key2 = Column(Integer, primary_key=True) attr1 = Column(Date) attr2 = Column(Boolean) related_b = Column(Integer, ForeignKey('B.spam')) class B(db.Model):

Fetch latest related objects in django

左心房为你撑大大i 提交于 2020-07-10 07:45:28
问题 In my django app I have 'Documents'. Each document has one or more 'Revisions' that are ordered by date created. I'd like a way to get the latest revision for every document. The best I have so far is the code below, but I'm thinking there must be a way to do this with less database queries? def get_document_templates(): result = [] for d in Document.objects.filter(is_template=True).all(): result.append(d.documentrevision_set.latest()) return result I've been investigating 'annotate' and

Django Rest Framework- retrieving a related field on reverse foreign key efficiently

落花浮王杯 提交于 2020-07-05 10:26:29
问题 I have the following models that represent a working group of users. Each working group has a leader and members: class WorkingGroup(models.Model): group_name = models.CharField(max_length=255) leader = models.ForeignKey(User, null=True, on_delete=models.SET_NULL) class WorkingGroupMember(models.Model): group = models.ForeignKey(WorkingGroup, on_delete=models.CASCADE) user = models.ForeignKey(User, on_delete=models.CASCADE) In DRF, I want to efficiently retrieve all groups (there are several

Django Rest Framework- retrieving a related field on reverse foreign key efficiently

老子叫甜甜 提交于 2020-07-05 10:25:18
问题 I have the following models that represent a working group of users. Each working group has a leader and members: class WorkingGroup(models.Model): group_name = models.CharField(max_length=255) leader = models.ForeignKey(User, null=True, on_delete=models.SET_NULL) class WorkingGroupMember(models.Model): group = models.ForeignKey(WorkingGroup, on_delete=models.CASCADE) user = models.ForeignKey(User, on_delete=models.CASCADE) In DRF, I want to efficiently retrieve all groups (there are several

How to avoid n+1 select in django?

安稳与你 提交于 2020-07-04 08:56:52
问题 I have a very simple datamodel with a one to many relationship between video and comments class Video(models.Model): url = models.URLField(unique=True) ..... class Comment(models.Model): title = models.CharField(max_length=128) video = models.ForeignKey('Video') ..... I want to query for videos and grab the whole object graph (videos with all the comments). Looking at the sql, I see it does two selects, one for the Videos and one for the Comments. How do I avoid that? I want to do a join and

Django filter location distance with dynamic radius

久未见 提交于 2020-06-30 23:57:27
问题 I have 2 model in django a zone and a shop, models are like this: from django.contrib.gis.db import models from django.contrib.gis.geos import Point from django.contrib.gis.measure import D from location_field.models.spatial import LocationField class Zone(models.Model): name = models.CharField(max_length=200) location_point = LocationField(based_fields=['city'], zoom=7, default=Point(51.67, 32.65)) radius = models.IntegerField(default=1000) # radius in meters class Shop(models.Model): name =

Database Model's meta data should be defined in models file or migration file?

巧了我就是萌 提交于 2020-06-28 06:57:11
问题 Using sequelize init I generated a model with a migration. Inside model.js there is only the definition of the type of each attribute, e.g. const User = sequelize.define('User', { firstName: DataTypes.STRING, lastName: DataTypes.STRING, email: DataTypes.STRING, password: DataTypes.STRING }, {}); In migration file, there are additional options e.g. ... up: (queryInterface, Sequelize) => { return queryInterface.createTable('Users', { id: { allowNull: false, autoIncrement: true, primaryKey: true

Many-to-many with Same Id Key fields

女生的网名这么多〃 提交于 2020-06-27 16:56:36
问题 How do I join two tables as many-to-one and one-to-many in nHibernate. They both have the same 'PLAN_ID' as the key column. My Database structures pre-existing. Below is the mapping I am using and the error I'm getting: Table: PLANN ( one ) PLAN_ID <-- PRIMARY KEY START_DATE CHECK_CHAR ... Table: PLANN_DOCUMENT ( to many ) PLAN_ID <-- PRIMARY KEY DOC_ID <-- ID to a DOCUMENT table DOC_NAME DOC_TYPE Plann class: public virtual... PlanId, StartDate, CheckChar, PlanStatus, public virtual ISet