2014-11-21 5 views
15

के लिए डिफ़ॉल्ट रूप से लैम्ब्डा का उपयोग करते समय Makemigrations विफल रहता है मैं Django 1.7.1 का उपयोग कर रहा हूँ। मेरा मॉडल इस तरह दिखता है:Django 1.7.1 गुण

from datetime import datetime 
from django.db import models 

class myModel(models.Model): 
    x = models.CharField(max_length=254,null=True, blank=True,) 

सबकुछ ठीक से काम करता है।

हालांकि, जब मैं myModel में निम्नलिखित एट्रीब्यूट जोड़ने, यह टूट जाता है:

y = models.DateTimeField(default=lambda: datetime.utcnow() + timedelta(days=1), editable=False) 

manage.py makemigrations मुझे निम्न त्रुटि देता है: http://comments.gmane.org/gmane.comp.python.django.scm/125724

:

ValueError: Cannot serialize function: lambda 

यह एक ज्ञात बग की तरह लगता है

तो मैं इसके आसपास कैसे काम कर सकता हूं? मॉडल के निर्माण के पल से 24 घंटे तक स्वचालित रूप से डिफ़ॉल्ट रूप से सेट होने के लिए मुझे y के मान की आवश्यकता है।

उत्तर

24

migrations documentation पते इस:

Migrations are just Python files containing the old definitions of your models - thus, to write them, Django must take the current state of your models and serialize them out into a file. While Django can serialize most things, there are some things that we just can’t serialize out into a valid Python representation....

Django can serialize the following: Any function or method reference

Django cannot serialize: Lambdas

तो समाधान सरल है: बस बल्कि एक लैम्ब्डा का उपयोग करने से मॉड्यूल दायरे में समारोह परिभाषित करते हैं।

+0

काम किया। धन्यवाद। –

+1

मैं आपके उत्तर पर ठोकर खाई। मेरे जैसे शुरुआती लोगों के लिए, हम नहीं जानते कि 'मॉड्यूल स्कोप में फ़ंक्शन को परिभाषित करें' से आपका क्या मतलब है। – Valachio

+0

@ वालाचियो इसका मतलब है कि उसी फ़ाइल में फ़ंक्शन को परिभाषित करें। यहां एक उदाहरण देखें: https://stackoverflow.com/a/15289517/490592 –

संबंधित मुद्दे