Skip to content

Instantly share code, notes, and snippets.

View egitimplus's full-sized avatar
😀

jure egitimplus

😀
View GitHub Profile
class Ceo(models.Model):
name = models.CharField(max_length=255, blank=True)
car = models.OneToOneField(Car, on_delete=models.SET_NULL, blank=True, null=True)
def __str__(self):
return self.name
@egitimplus
egitimplus / models.py
Created November 2, 2020 11:36
OneToOneField
from django.db import models
class Car(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
@egitimplus
egitimplus / models.py
Created November 2, 2020 10:49
OneToOneField
from django.db import models
class Car(models.Model):
name = models.CharField(max_length=255)
class Ceo(models.Model):
car = models.OneToOneField(Car, on_delete=models.CASCADE)
name = models.CharField(max_length=255, blank=True)
@egitimplus
egitimplus / models.py
Last active October 27, 2020 15:09
Many-to-many relationships
class FuelType(models.Model):
name = models.CharField(max_length=255)
class CarModel(models.Model):
name = models.CharField(max_length=255)
car = models.ForeignKey(Car, on_delete=models.CASCADE)
fueltype = models.ManyToManyField(FuelType)
@egitimplus
egitimplus / models.py
Created October 27, 2020 15:07
Many-to-one relationships
class Car(models.Model):
name = models.CharField(max_length=255)
class CarModel(models.Model):
name = models.CharField(max_length=255)
car = models.ForeignKey(Car, on_delete=models.CASCADE)
@egitimplus
egitimplus / models.py
Last active October 27, 2020 15:06
One-to-one relationships
class Car(models.Model):
name = models.CharField(max_length=255)
class CarInformation(models.Model):
car = models.OneToOneField(Car, on_delete=models.CASCADE)
address = models.TextField(blank=True)
phone = models.CharField(max_length=15, blank=True)
@egitimplus
egitimplus / serializers.py
Created October 26, 2020 17:20
UserProfileSerializer
from .models import UserProfile
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ('user', 'bio', 'address', 'phone', 'gender')
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
from django.apps import AppConfig
class AuthConfig(AppConfig):
name = 'auth'
label = 'my_auth'
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)
bio = models.TextField(blank=True)
address = models.TextField(blank=True)
phone = models.CharField(max_length=15, blank=True)