Giter Club home page Giter Club logo

Comments (3)

coleifer avatar coleifer commented on June 11, 2024

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.

igoncharov85 avatar igoncharov85 commented on June 11, 2024

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.

coleifer avatar coleifer commented on June 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.