Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't support with random objects, order_by('?') #13

Open
agusmakmun opened this issue May 26, 2021 · 1 comment · May be fixed by #14
Open

Doesn't support with random objects, order_by('?') #13

agusmakmun opened this issue May 26, 2021 · 1 comment · May be fixed by #14

Comments

@agusmakmun
Copy link

agusmakmun commented May 26, 2021

In [11]: from practical_foobar_egg_labs.courses.models.course import CourseModel

In [12]: from practical_foobar_egg_labs.courses.utils.storage import CourseData

In [13]: courses = CourseData().get_courses()
INFO 2021-05-26 00:00:40,271 storage 1 139861811062592 [storage.py:87] [queryset_courses] Retrieved courses from cache.

In [14]: courses
Out[14]: 
[{'title': 'Foobar Foundation Course',
  'description': 'The Foobar Foundation course is our beginner course which prepares you for the professional, expert and architect courses',
  'image': 'cdp2.png',
  'keywords': ['SAST', 'DAST', 'IaC', 'SCA'],
  'flow': ['linux-basics',
   'docker',
   'continuous-integration-and-continous-delivery'],
  'details': {'steps': [{'title': 'Step 1 - Download the code from the repo',
     'text': 'step1.md'},
    {'title': 'Step 2 - Install the TruffleHog', 'text': 'step2.md'},
    {'title': 'Step 3 - Run the SAST Scan', 'text': 'step3.md'}],
   'intro': {'text': 'intro.md'},
   'finish': {'text': 'finish.md'}},
  'course_slug': 'foobar-foundation'},
 {'title': 'Python Foundation Course',
  'description': 'The Python Foundation course is our beginner course which prepares you for the professional, expert and architect courses',
  'image': 'cdp2.png',
  'keywords': ['Python', 'Programming'],
  'flow': ['python-basics', 'python-intermediate', 'python-professional'],
  'details': {'steps': [{'title': 'Step 1 - Download the code from the repo',
     'text': 'step1.md'},
    {'title': 'Step 2 - Install the TruffleHog', 'text': 'step2.md'},
    {'title': 'Step 3 - Run the SAST Scan', 'text': 'step3.md'}],
   'intro': {'text': 'intro.md'},
   'finish': {'text': 'finish.md'}},
  'course_slug': 'python-foundation'}]

In [15]: 

In [15]: for course_data in courses:
    ...:     course_slug = course_data.pop('course_slug', None)
    ...:     CourseModel.objects.get_or_create(
    ...:         course_slug=course_slug,
    ...:         defaults=course_data
    ...:     )
    ...: 

In [16]: 
In [17]: CourseModel.objects.all()
Out[17]: 
[CourseModel(course_slug='foobar-foundation', title='Foobar Foundation Course', description='The Foobar Foundation course is our beginner course which prepares you for the professional, expert and architect courses', image='cdp2.png', keywords=['SAST', 'DAST', 'IaC', 'SCA'], flow=['linux-basics', 'docker', 'continuous-integration-and-continous-delivery'], details={'steps': [{'title': 'Step 1 - Download the code from the repo', 'text': 'step1.md'}, {'title': 'Step 2 - Install the TruffleHog', 'text': 'step2.md'}, {'title': 'Step 3 - Run the SAST Scan', 'text': 'step3.md'}], 'intro': {'text': 'intro.md'}, 'finish': {'text': 'finish.md'}}),
 CourseModel(course_slug='python-foundation', title='Python Foundation Course', description='The Python Foundation course is our beginner course which prepares you for the professional, expert and architect courses', image='cdp2.png', keywords=['Python', 'Programming'], flow=['python-basics', 'python-intermediate', 'python-professional'], details={'steps': [{'title': 'Step 1 - Download the code from the repo', 'text': 'step1.md'}, {'title': 'Step 2 - Install the TruffleHog', 'text': 'step2.md'}, {'title': 'Step 3 - Run the SAST Scan', 'text': 'step3.md'}], 'intro': {'text': 'intro.md'}, 'finish': {'text': 'finish.md'}})]

In [18]: CourseModel.objects.all().order_by('?')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-18-71eea73a8983> in <module>
----> 1 CourseModel.objects.all().order_by('?')

/usr/local/lib/python3.8/site-packages/reobject/query/queryset.py in order_by(self, *fields)
    161 
    162         return type(self)(
--> 163             sorted(self, key=cmp(*fields)),
    164             model=self.model
    165         )

/usr/local/lib/python3.8/site-packages/reobject/utils.py in g(obj)
     11 
     12     def g(obj):
---> 13         return tuple(
     14             resolve_attr(obj, attr) for attr in items
     15         )

/usr/local/lib/python3.8/site-packages/reobject/utils.py in <genexpr>(.0)
     12     def g(obj):
     13         return tuple(
---> 14             resolve_attr(obj, attr) for attr in items
     15         )
     16 

/usr/local/lib/python3.8/site-packages/reobject/utils.py in resolve_attr(obj, attr)
     26             obj = obj.get(name)
     27         else:
---> 28             obj = getattr(obj, name)
     29 
     30     return obj if not reverse else -obj

AttributeError: 'CourseModel' object has no attribute '?'

In [19]: 
@agusmakmun agusmakmun changed the title Doesn't support with random objects, `order_by( Doesn't support with random objects, order_by('?') May 26, 2021
@agusmakmun
Copy link
Author

agusmakmun commented May 26, 2021

.random() only select 1 object, but I want do more objects.

CourseModel.objects.random()

@agusmakmun agusmakmun reopened this May 26, 2021
@agusmakmun agusmakmun linked a pull request May 26, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant