Skip to content

Instantly share code, notes, and snippets.

View TSKGunGun's full-sized avatar
💭
I may be slow to respond.

Suguru Ueda TSKGunGun

💭
I may be slow to respond.
View GitHub Profile
class sample_Test extends TestCase
{
use RefreshDatabase;
use WithFaker;
protected function setUp(): void
{
parent::setUp();
Category::factory(5)->create();
}
def getdata(request):
products = []
for item in Product.objects.all:
product = {
"id" : item.id,
"name" : item.name
}
products.append(product)
def getdata(request):
products = []
for item in Product.objects.all:
product = {
"id" : item.id,
"name" : item.name
}
products.append(product)
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
def getdata(request):
products = []
for item in Product.objects.all:
product = {
"id" : item.id,
"name" : item.name
}
products.append(product)
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:
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)
from django import forms
from .models import Foo
class FooForm(forms.ModelForm):
class Meta:
model = Foo
fields = ('num', )
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).
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