def create(request): try: data = request.data try: print(data['username'], data['email']) User = get_user_model() user = User.objects.create_user(username=data['username'], email=data['email'], is_staff=True) user.set_password(data['password']) user.save() token, _ = Token.objects.get_or_create(user=user) profile = UserProfile() profile.user = user profile.email = data.get("email") profile.save() except Exception as e: print("Exception is ", e) return Response({"status": str(e)}) return Response({"status": "success"}, status=HTTP_200_OK) except Exception as e: print("Exception is ", e) return Response({"status": "failed", "Exception": str(e)})
class UserProfileViewSet(ReadOnlyModelViewSet): queryset = get_user_model().objects.all() serializer_class = serializers.UserSerializer permission_classes = (IsAuthenticated,) @list_route(methods=['get']) def register(self, request, format=None): return Response({ 'msg': 'To test this application send an email to the system administrator. Registrations are disabled for the moment.'})
def getalluser(request): userdata = UserProfile.objects.all() resp = {} for i in userdata: User = get_user_model() name = User.objects.get(username=i.user) resp[str(name)] = {} t = {} t["user"] = str(name) t["email"] = i.email resp[str(name)] = t return Response(resp)
def create(request, user): data = request.data try: User = get_user_model() user = User.objects.get(username=user) store_profile = stores() store_profile.name = data["name"] store_profile.description = data["description"] store_profile.location = data["location"] store_profile.center = data["center"] store_profile.image = data["image"] store_profile.time = json.dumps(data["time"]) store_profile.range = json.dumps(data["range"]) store_profile.creator = user store_profile.save() except Exception as e: print("Exception is ", e) return Response({"status": str(e)}) return Response({"status": "success"}, status=HTTP_200_OK)
def getstores(request, user): l = user User = get_user_model() user = User.objects.get(username=user) store = stores.objects.filter(creator=user) resp = {} for i in store: # try: resp[i.id] = {} # except Exception as e: # print(e) temp = {} temp["name"] = i.name temp["description"] = i.description temp["location"] = i.location temp["center"] = i.center temp["image"] = i.image temp["time"] = json.loads(i.time) temp["range"] = json.loads(i.range) temp["creator"] = l temp["date"] = str(i.created_date) resp[i.id] = temp return Response(resp)
class stores(models.Model): name = models.CharField(max_length=255, unique=True) description = models.CharField(max_length=255, default="", null=True, blank=True) location = models.CharField(max_length=255, default="", null=True, blank=True) center = models.CharField(max_length=255, default="", null=True, blank=True) image = models.TextField(default="", null=True, blank=True) time = models.TextField(default={}, null=True, blank=True) range = models.TextField(default=[], null=True, blank=True) created_date = models.DateTimeField(default=timezone.now) # creator = models.CharField(max_length=255,default="",null=True,blank=True) creator = models.ForeignKey(get_user_model(), on_delete=models.CASCADE) def __str__(self): return str(self.name) + "_" + str(self.id)
# -*- coding: utf-8 -*- from __future__ import unicode_literals from rest_framework.authentication import get_user_model from django.db import models # Create your models here. User = get_user_model()
from rest_framework.authentication import get_user_model from django.conf import settings from django.db.models.signals import post_save from django.dispatch import receiver from rest_framework.authtoken.models import Token User = get_user_model() # Модель юзера. @receiver(post_save, sender=settings.AUTH_USER_MODEL) def create_auth_token(sender, instance=None, created=False, **kwargs): if created: Token.objects.create(user=instance)
from django.db import models # Create your models here. from goods.models import Goods from rest_framework.authentication import get_user_model User = get_user_model() # 获取获取到用户类 from datetime import datetime class UserFav(models.Model): """ 用户收藏操作 """ user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="用户") goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品", help_text="商品id") add_time = models.DateTimeField("添加时间",default=datetime.now) class Meta: verbose_name = '用户收藏' verbose_name_plural = verbose_name unique_together = ("user", "goods") # 定义联合唯一 def __str__(self): return self.user.username class UserAddress(models.Model): """ 用户收货地址 """
class Meta: model = get_user_model() fields = ('url', 'username', 'first_name', 'last_name', 'email')