Skip to content

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

Description

@agusmakmun
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]: 

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions