Skip to content

Instantly share code, notes, and snippets.

@gabrielloliveira
Last active September 29, 2017 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielloliveira/81d8ba8b865a387f817a363bb53e1473 to your computer and use it in GitHub Desktop.
Save gabrielloliveira/81d8ba8b865a387f817a363bb53e1473 to your computer and use it in GitHub Desktop.
from django.contrib import admin
from .models import Perfil
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
# Register your models here.
class PerfilInline(admin.StackedInline):
model = Perfil
can_delete = False
verbose_name_plural = 'perfil'
class UserAdmin(BaseUserAdmin):
inlines = (PerfilInline, )
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
from django.db import models
from django.contrib.auth.models import User
from django.dispatch import receiver
from django.db.models.signals import post_save
class Perfil(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
idade = models.IntegerField(blank=True, default=0)
@receiver(post_save, sender=User)
def criar_perfil(sender, instance, created, **kwargs):
if created:
Perfil.objects.create(user=instance)
@receiver(post_save, sender=User)
def salvar_perfil(sender, instance, **kwargs):
instance.perfil.save()
from django.shortcuts import render
from django.contrib.auth.models import User
from django.http import HttpResponse
# Create your views here.
def update(request):
usuario = request.POST['usuario']
email = request.POST['email']
senha = request.POST['senha']
idade = request.POST['idade']
novoUsuario = User.objects.create_user(username=usuario,
email=email,
password=senha)
novoUsuario.save()
novoUsuario.perfil.idade = idade
novoUsuario.save()
return HttpResponse("Cadastrado com sucesso.", content_type="text/plain")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment