Comments (3)
This is, to me, not an issue with Peewee but a deficiency in the tool you're using. To modify one's code in order to workaround limitations in an IDE is, of course, your call... but you would:
User.tweets = BackrefAccessor(Tweet.user)
I don't recommend this kind of thing one bit, though, because it's redundant and may make future changes to either Tweet or User fragile (e.g. if you wanted to change the backref name later, or something).
from peewee.
Thanks for the answer.
This is the code I'm trying to use BackrefAccessor
in
import peewee
from peewee import *
db = SqliteDatabase(':memory:')
print(peewee.__version__)
class DbModel(Model):
class Meta:
database = db
class UserData(DbModel):
UserId = AutoField(primary_key=True)
Login = CharField()
Teacher = peewee.BackrefAccessor(Teacher.UserData)
Student = peewee.BackrefAccessor(Student.UserData)
class Teacher(DbModel):
SchoolName = CharField()
UserData = ForeignKeyField(UserData, backref='Teacher', primary_key=True, object_id_name='TeacherId')
class Student(DbModel):
University = CharField()
UserData = ForeignKeyField(UserData, backref='Student', primary_key=True, object_id_name='StudentId')
db.create_tables([UserData, Teacher, Student])
with db.atomic():
user_data = UserData.create(Login='user_login')
teacher = Teacher.create(SchoolName='School1', UserData=user_data)
student = Student.create(University='University1', UserData=user_data)
assert user_data.UserId == student.StudentId
assert user_data.Student == student
assert user_data.UserId == teacher.TeacherId
assert user_data.Teacher == teacher
How do I reference the class defined later in the code?
3.16.2
Traceback (most recent call last):
File "C:\repos\GitHub\ukd-orm\peewee_orm\peewee_deferred.py", line 11, in <module>
class UserData(DbModel):
File "C:\repos\GitHub\ukd-orm\peewee_orm\peewee_deferred.py", line 15, in UserData
Teacher = peewee.BackrefAccessor(Teacher.UserData)
^^^^^^^
NameError: name 'Teacher' is not defined
from peewee.
How do I reference the class defined later in the code?
Exactly. Just let peewee do it for you. If you aren't willing to do that, you need to wait until after the classes are defined and put the backref accessors on as I showed in my example code.
from peewee.
Related Issues (20)
- How to simply replace in postgres? HOT 1
- Incorporating pgvector for document/similarity search? HOT 1
- Wrong MySQL error HOT 1
- Adding Support for jsonb_array_elements in Peewee: Discussion and Contribution Proposal HOT 3
- Full path to alias? HOT 3
- ManyToMany Fields Model init hint many unnecessary query HOT 3
- Cython 3 support HOT 3
- Problematic rollback semantics with nested transactions HOT 9
- Selected relations not included in the final result. HOT 3
- bulk_update update the field with wrong value HOT 5
- 500 internal error on every .py page of Roxy-WI HOT 2
- Duplicate PKEY even when setting the id_seq HOT 16
- The query operator .between in https://docs.peewee-orm.com/en/latest/peewee/query_operators.html does not state if it includes boundaries or not HOT 2
- Property that does calculation DateTime + minutes HOT 1
- Add `__getitem__` to `ColumnFactory`
- Prefetch with union subquery HOT 3
- Clarification needed in peewee documentation query operators
- Addition fn.DATETIME HOT 1
- Please revert the docs style to the previous version HOT 1
- Model.update() does not preserve field order HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from peewee.