English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

نموذج دجونو (Model)

النموذج هو تمثيل جدول قاعدة البيانات أو فئة المجموعات، وكل خاصية في هذه الفئة هي حقل في الجدول أو المجموعة. يتم تعريف النموذج في app/models.py (في مثالنا هو: myapp/models.py)

إنشاء نموذج

فيما يلي إنشاء نموذج Dreamreal -

# اسم الملف: example.py
# حقوق التأليف: 2020 بواسطة w3codebox
# مؤلف: ar.oldtoolbag.com
# تاريخ: 2020-08-08
from django.db import models
 class Dreamreal(models.Model):
    website = models.CharField(max_length = 50)
    mail = models.CharField(max_length = 50)
    name = models.CharField(max_length = 50)
    phonenumber = models.IntegerField()
    class Meta:
       db_table = "dreamreal"

كل نموذج يرث من django.db.models.Model.

لدينا 4 خصائص (3 CharField و 1 عدد صحيح)، هذه ستكون الحقول في الجدول.

The Meta class and the db_table attribute allow us to define the actual table or collection name. Django will automatically name the table or collection: myapp_modelName. This class will enforce the table name. Django will automatically name the table or collection: myapp_modelName. This class will enforce the table name.

For more field types in django.db.models, you can learn more about them at the following URL:

https://docs.djangoproject.com/en/1.5/ref/models/fields/#field-types

After creating the model, Django needs to generate the actual database -

# اسم الملف: example.py
# حقوق التأليف: 2020 بواسطة w3codebox
# مؤلف: ar.oldtoolbag.com
# تاريخ: 2020-08-08
$python manage.py syncdb

Data Operations (CRUD)

Let's create a 'crudops' view to see how CRUD operations can be performed on the model. Now myapp/views.py will look like -

myapp/views.py

# اسم الملف: example.py
# حقوق التأليف: 2020 بواسطة w3codebox
# مؤلف: ar.oldtoolbag.com
# تاريخ: 2020-08-08
from myapp.models import Dreamreal
 from django.http import HttpResponse
 def crudops(request):
    #Creating an entry
    dreamreal = Dreamreal(
       website = "www.polo.com", mail = "[email protected]", 
       name = "sorex", phonenumber = "002376970"
    )
    dreamreal.save()
    #Read ALL entries
    objects = Dreamreal.objects.all()
    res = 'Printing all Dreamreal entries in the DB: <br>'
    for elt in objects:
       res += elt.name + '<br>'
    #Read a specific entry:
    sorex = Dreamreal.objects.get(name = "sorex")
    res += 'Printing One entry <br>'
    res += sorex.name
    #Delete an entry
    res += '<br> Deleting an entry <br>'
    sorex.delete()
    #Update
    dreamreal = Dreamreal(
       website = "www.polo.com", mail = "[email protected]", 
       name = "sorex", phonenumber = "002376970"
    )
    dreamreal.save()
    res += 'Updating entry<br>'
    dreamreal = Dreamreal.objects.get(name = 'sorex')
    dreamreal.name = 'thierry'
    dreamreal.save()
    return HttpResponse(res)

عمليات بيانات أخرى

لنستكشف الأنشطة الأخرى التي يمكن القيام بها مع النماذج. يجب الانتباه إلى أن عمليات CRUD كلها تتم على نماذج الفئات، وسنقوم بتوضيح عمل نماذج الفئات مباشرة.

لنقم بإنشاء "datamanipulation" في myapp/views.py

# اسم الملف: example.py
# حقوق التأليف: 2020 بواسطة w3codebox
# مؤلف: ar.oldtoolbag.com
# تاريخ: 2020-08-08
from myapp.models import Dreamreal
 from django.http import HttpResponse
 def datamanipulation(request):
    res = ''
    #تقييد البيانات:
    qs = Dreamreal.objects.filter(name = "paul")
    res += "Found : %s results<br>"%len(qs)
    #ترتيب النتائج
    qs = Dreamreal.objects.order_by("name")
    for elt in qs:
       res += elt.name + '<br>'
    return HttpResponse(res)

ربط النماذج

يقدم Django ORM ثلاث طرق للربط بين النماذج −

في هذا المثال الأول الذي سنراه هنا هو علاقة اثنين إلى متعدد. كما رأينا في المثال السابق، يمكن لشركة أن تملك عدة مواقع على الإنترنت. يتم تعريف هذه العلاقة باستخدام django.db.models.ForeignKey -

myapp/models.py

# اسم الملف: example.py
# حقوق التأليف: 2020 بواسطة w3codebox
# مؤلف: ar.oldtoolbag.com
# تاريخ: 2020-08-08
from django.db import models
 class Dreamreal(models.Model):
    website = models.CharField(max_length = 50)
    mail = models.CharField(max_length = 50)
    name = models.CharField(max_length = 50)
    phonenumber = models.IntegerField()
    online = models.ForeignKey('Online', default = 1)
    class Meta:
       db_table = "dreamreal"
 class Online(models.Model):
       domain = models.CharField(max_length = 30)
    class Meta:
       db_table = "online"

يمكنك تحديث myapp/models.py، كما ترونه، لقد أضفنا نموذجًا عبر الإنترنت وربطناه بنموذج Dreamreal.

دعنا نرى كيف يمكننا تنفيذ جميع العمل عبر manage.py shell

دعنا نبدأ في اختبار Django shell لإنشاء بعض الشركات ( Dreamreal إصدار)

# اسم الملف: example.py
# حقوق التأليف: 2020 بواسطة w3codebox
# مؤلف: ar.oldtoolbag.com
# تاريخ: 2020-08-08
$python manage.py shell
 >>> from myapp.models import Dreamreal, Online
 >>> dr1 = Dreamreal()
 >>> dr1.website = 'company1.com'
 >>> dr1.name = 'company1'
 >>> dr1.mail = 'contact@company1'
 >>> dr1.phonenumber = '12345'
 >>> dr1.save()
 >>> dr2 = Dreamreal()
 >>> dr1.website = 'company2.com'
 >>> dr2.website = 'company2.com'
 >>> dr2.name = 'company2'
 >>> dr2.mail = 'contact@company2'
 >>> dr2.phonenumber = '56789'
 >>> dr2.save()

الآن هناك بعض نطاقات الشبكة الوسيطة

# اسم الملف: example.py
# حقوق التأليف: 2020 بواسطة w3codebox
# مؤلف: ar.oldtoolbag.com
# تاريخ: 2020-08-08
>>> on1 = Online()
 >>> on1.company = dr1
 >>> on1.domain = "site1.com"
 >>> on2 = Online()
 >>> on2.company = dr1
 >>> on2.domain = "site2.com"
 >>> on3 = Online()
 >>> on3.domain = "site3.com"
 >>> dr2 = Dreamreal.objects.all()[2]
 >>> on3.company = dr2
 >>> on1.save()
 >>> on2.save()
 >>> on3.save()

من البداية عبر النطاق المضيف (Dreamreal إصدار) بسيط جداً

 # اسم الملف: example.py
# حقوق التأليف: 2020 بواسطة w3codebox
# مؤلف: ar.oldtoolbag.com
# تاريخ: 2020-08-08
>>> on1.company.name

إذا كنت ترغب في معرفة جميع نطاقات الإنترنت على الإنترنت التي ينظمها Dreamreal الشركة، سنستخدم كودًا مثل −

# اسم الملف: example.py
# حقوق التأليف: 2020 بواسطة w3codebox
# مؤلف: ar.oldtoolbag.com
# تاريخ: 2020-08-08
>>> dr1.online_set.all()

للحصول على QuerySet، يرجى ملاحظة أن جميع الطرق التي رأيناها (filter, all, exclude, order_by...)

يمكن أيضًا الوصول إلى تنفيذ عمليات التحقق على خصائص الروابط، على سبيل المثال، للحصول على جميع المجالات على الإنترنت الموجودة في Dreamreal التي تحتوي على "company".

# اسم الملف: example.py
# حقوق التأليف: 2020 بواسطة w3codebox
# مؤلف: ar.oldtoolbag.com
# تاريخ: 2020-08-08
>>> Online.objects.filter(company__name__contains='company')

ملاحظة - هذه الاستعلامات تدعم قواعد البيانات SQL فقط. لن تعمل على قواعد البيانات غير العلاقاتية، حيث لا توجد روابط، وثمة كلمتان "_".

لكن، هذا ليس الطريقة الوحيدة للروابط بين النماذج، هناك أيضًا OneToOneField، التي تضمن أن تكون علاقة بين العنصرين مرتبطة بشكل فريد. إذا تم استخدام OneToOneField في المثال السابق، فإن هذا يعني أن هناك مساهمة واحدة فقط على الخطوط تناسب كل مساهمة Dreamreal.

الآخر، هذه هي علاقات NN بين جداول ManyToManyField هذه قائمة على قاعدة بيانات SQL.