(5) ورشة تطوير تطبيقات الويب باستخدام Django: التعامل مع Django ORM


#41

بعد حذف ملف قاعدة البيانات كيف استعيده
من خلال python manage.py makemigrations shop
؟؟؟؟؟؟؟؟


#42

تم اصلاح المشكل و هذا ما فعلته :
-مسحت مجلد migrations

  • فتحت قاعدة البيانات من sqlitebrowser فوجدت shop_product لا يزال موجودا فمسحته
    -قمت ب write changes لحفظ التغييرات
    -اعدت المراحل بصفة عادية python manage.py makemigrations shop

ثم python manage.py migrate


#43

Makemigrations تقوم بعمل الmigrations أي تعمل مخطط لقاعدة البيانات على حسب محتوى الملف models.py
ولك الأمر migrate هو من ينشئ قاعدة البيانات على حسب ما هو موجود في ملفات المجلد migrations

لهذا بعد عمل makemigrations يجب التأكد من الmigration لترى إن كان محتواه هو حقا ما تريده في قاعدة البيانات ام ان هناك خطأ ما


#44

شكرا و الله تعبتكم معايا … انا على طول نفس الاخطاء البسيطة و تخليني داخل بعضي :smile:


#45

أبدا أخي سيف، لا تعب ولا شيء، كلنا نخطئ ونصيب، الشيء المهم هو المواصلة وعدم الإستسلام


#46

after finish write the category object and product object and make save when i used
object name-of-filed
it doesn’t return any thing
it give me name c1 is not defined
please any one tell me where is error


#47

هل قمت بعمل

from shop.models import Product, Category

have you defined c1 first ??


#48

yes i define c1 and p1,p2,p3
and made save for all this object and all of them give me the same error after i close the shell and open shell again


#49

this is error which face me


#50

you didn’t define c1 for example
you have to put:

from shop.models import Category, Product
>>> 
>>> c1 = Category(
...         name="Mobile Devices",
...         slug="mobile-devices",
...         description="This category will contains mobile devices.")
>>> c1.save()

#51

i define it at first and then i close the laptop and when i open the computer again and write c1.name it give me these error


#52

no, you have to define an object to work with
you can create another one
or call it from the database like this

q=Category.objects.get(pk=1)

then you can play with it

q.name
q.description

etc …


#53

thank you it work well now
but i need from you to describe this code for me and what is (pk=1) refer
and why when i use p1.name didn’t work can you describe for me please


#54

you can use (id=1) instead
you can reread the topic and you will understand more, you need to focus
p1.name didnt work because you didn’t define p1,
you just defined q
if you write
q.name it will work


#55

thank you for your answer
but i define p1,p2,p3 and c1


#56

if you defined it and it still not working, upload a picture of the error to fix it


#57

>>> p1.name
Traceback (most recent call last):
** File “”, line 1, in **
NameError: name ‘p1’ is not defined
this is the error display after define all objects,and save it and after close the git shell and open it again


#58

if you close the shell
you have to import modules again and define objects too


#59

this means that p1,p2,p3 and c1 all of them are variables and when i open shell again i need to assign data again to this variable

thank you for your great effort


#60

yes that’s it, when you run the shell again , you have to define objects again too