This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class sample_Test extends TestCase | |
{ | |
use RefreshDatabase; | |
use WithFaker; | |
protected function setUp(): void | |
{ | |
parent::setUp(); | |
Category::factory(5)->create(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getdata(request): | |
products = [] | |
for item in Product.objects.all: | |
product = { | |
"id" : item.id, | |
"name" : item.name | |
} | |
products.append(product) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getdata(request): | |
products = [] | |
for item in Product.objects.all: | |
product = { | |
"id" : item.id, | |
"name" : item.name | |
} | |
products.append(product) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JsonResponse(HttpResponse): | |
""" | |
An HTTP response class that consumes data to be serialized to JSON. | |
:param data: Data to be dumped into json. By default only ``dict`` objects | |
are allowed to be passed due to a security flaw before EcmaScript 5. See | |
the ``safe`` parameter for more information. | |
:param encoder: Should be a json encoder class. Defaults to | |
``django.core.serializers.json.DjangoJSONEncoder``. | |
:param safe: Controls if only ``dict`` objects may be serialized. Defaults |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getdata(request): | |
products = [] | |
for item in Product.objects.all: | |
product = { | |
"id" : item.id, | |
"name" : item.name | |
} | |
products.append(product) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def run_validators(self, value): | |
if value in self.empty_values: | |
return | |
errors = [] | |
for v in self.validators: | |
try: | |
v(value) | |
except exceptions.ValidationError as e: | |
if hasattr(e, 'code') and e.code in self.error_messages: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def full_clean(self, exclude=None, validate_unique=True): | |
""" | |
Call clean_fields(), clean(), and validate_unique() on the model. | |
Raise a ValidationError for any errors that occur. | |
""" | |
errors = {} | |
if exclude is None: | |
exclude = [] | |
else: | |
exclude = list(exclude) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from .models import Foo | |
class FooForm(forms.ModelForm): | |
class Meta: | |
model = Foo | |
fields = ('num', ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _post_clean(self): | |
opts = self._meta | |
exclude = self._get_validation_exclusions() | |
# Foreign Keys being used to represent inline relationships | |
# are excluded from basic field value validation. This is for two | |
# reasons: firstly, the value may not be supplied (#12507; the | |
# case of providing new values to the admin); secondly the | |
# object being referred to may not yet fully exist (#12749). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def errors(self): | |
"""Return an ErrorDict for the data provided for the form.""" | |
if self._errors is None: | |
self.full_clean() | |
return self._errors | |
def is_valid(self): | |
"""Return True if the form has no errors, or False otherwise.""" | |
return self.is_bound and not self.errors |
NewerOlder