我有一个叫预订的模型。它具有start_time和end_time字段。它也有一个is_active领域。
现在,假设预订有start_time = 2017/Nov/22 12:00:00和end_time = 2017/Nov/22 14:00:00。
现在,当服务器崩溃时,
1-如果当前时间在开始时间和结束时间之间,则is_active应该为True。
2-当前时间大于end_time或小于预订的start_time,我要设置is_active = False。
我希望它在后台连续运行,以便它必须在数据库中实时保持预订is_active状态。如何在django中做到这一点?
我正在开发Django购物车应用程序。我有两种型号的购物车和物品。我正在尝试将数量添加到购物篮中时更新数量,但无法使视图正常工作。我在使item_obj分配工作时遇到问题-在这里我需要对模型管理器做任何事情吗?任何帮助都非常感谢。
型号
class Cart(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
products = models.ManyToManyField(Product, blank=True)
total = models.DecimalField(default=0.00, max_digits=10, decimal_places=2)
updated = models.DateTimeField(auto_now=True)
timestamp = models.DateTimeField(auto_now_add=True)
objects = CartManager()
def __str__(self):
return str(self.id)
class Item(models.Model):
item = models.ForeignKey(Product, null=True)
cart = models.ForeignKey(Cart, null=True)
quantity = models.PositiveIntegerField()
Run Code Online (Sandbox Code Playgroud)
Views.py提取
def cart_update(request):
product_id = request.POST.get('product_id')
product_obj = Item.objects.get(id=product_id)
print(item_id)
item_obj = Item.objects.get(id=product_id)
cart_obj, new_obj = Cart.objects.new_or_get(request)
if item_obj in cart_obj.products.all():
cart_obj.products.add(product_obj)
item_obj.quantity += 1
item_obj.save()
else:
cart_obj.products.add(product_obj)
return redirect("cart:home")
EDIT
Run Code Online (Sandbox Code Playgroud)
Views.py更新(12/02/2018)
def cart_update(request):
# Based …Run Code Online (Sandbox Code Playgroud) 我在写一个Django项目.
在 courses/models.py
class Category(models.Model):
title = models.CharField(max_length=50)
class Language(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
title = models.CharField(max_length=50)
class Course(models.Model):
name = models.CharField(max_length=250)
language = models.ForeignKey(Language, on_delete=models.CASCADE)
Run Code Online (Sandbox Code Playgroud)
我想得到所有列表,Category然后遍历每个Language指定的类别.
class Courses(ListView):
template_name = 'courses/index.html'
model = Course
context_object_name = 'courses'
def get_context_data(self, **kwargs):
context = super(Courses, self).get_context_data(**kwargs)
categories = Category.objects.all()
context['categories'] = categories
return context
Run Code Online (Sandbox Code Playgroud)
在模板中courses/index.html我想显示基于类别的语言列表
{% for category in categories %}
{{ category.title }}
<li>lis of languages in this category</li>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
如何循环反向关联数据?
I am making a CRUD application, and I have the current piece of code in my view:
def dashboard(request):
template = 'dashboard/index.html'
form = CustomerForm()
if request.user.groups.filter(name__in=['West']).exists():
customers = Customer.objects.filter(Q(department='630') | Q(department='635')).all()
elif request.user.groups.filter(name__in=['North']).exists():
customers = Customer.objects.filter(Q(department='610') | Q(department='615') | Q(department='620')).all()
elif request.user.groups.filter(name__in=['East']).exists():
customers = Customer.objects.filter(Q(department='660') | Q(department='655') | Q(department='650')).all()
elif request.user.groups.filter(name__in=['South']).exists():
customers = Customer.objects.filter(Q(department='640') | Q(department='645')).all()
elif request.user.groups.filter(name__in=['North-West']).exists():
customers = Customer.objects.filter(Q(department='625')).all()
else:
customers = Customer.objects.all()
context = {
"customers": customers,
"form": form,
}
return render(request, template, context)
Run Code Online (Sandbox Code Playgroud)
I have …
我有使用电子邮件作为身份验证的唯一标识符而不是用户名的自定义用户模型。我想通过电子邮件或手机号码注册用户。如果用户输入电子邮件地址,则通过激活链接注册用户;如果用户输入电话号码,则通过SMS OTP注册。
像instagram注册这样的东西:
https://www.instagram.com/accounts/emailsignup/
我找到了这个主题,但是答案却解释得不好。
有一个让我感到烦恼的问题,我想做的就是当我请求detail.html时,Post模型的视图将在访问计数中加1,该怎么做?谢谢。
博客/models.py
class Post(models.Model):
views = models.PositiveIntegerField(default=0)
Run Code Online (Sandbox Code Playgroud)
博客/views.py
def detail(request):
return render(request, 'blog/detail.html')
Run Code Online (Sandbox Code Playgroud) 在我的models.py中,我有:
def MakeOTP():
import random,string
return ''.join(random.choices(string.digits, k=4))
class Prescriptionshare(models.Model):
prid = models.AutoField(primary_key=True, unique=True)
customer = models.ForeignKey(
customer, on_delete=models.CASCADE, null=True)
time = models.DateTimeField(default=timezone.now)
checkin =models.ForeignKey(Checkins, on_delete=models.CASCADE, null=True)
otp = models.CharField(max_length=5, default=MakeOTP())
Run Code Online (Sandbox Code Playgroud)
在我的django shell中,我尝试了以下内容:
pq = Prescriptionshare(customer = cus, checkin = chk)
pq.save()
Run Code Online (Sandbox Code Playgroud)
问题是,每次执行此操作时,我都会在otp字段中获得相同的字符串.字符串没有随机变化.为什么会这样?
我在更新时遇到错误.当我添加数据时,它会成功添加.这个错误只是为了UpdateAPIView
{
"detail": "Method \"POST\" not allowed."
}
Run Code Online (Sandbox Code Playgroud)
urls.py
path('groups/update/<int:pk>', views.GroupsUpdateAPIView.as_view(), name='api_groups_update'),
Run Code Online (Sandbox Code Playgroud)
Views.py
class GroupsUpdateAPIView(generics.UpdateAPIView):
queryset = Groups.objects.all()
serializer_class = GroupsAddSerialzer
permission_classes = [UserIsAuthenticated]
def perform_update(self, serializer):
serializer.save(
group_updated_by = self.request.auth.application.user,
)
Run Code Online (Sandbox Code Playgroud)
Serializer.py
class GroupsAddSerialzer(serializers.ModelSerializer):
class Meta:
model = Groups
fields = ['group_name', 'group_description', 'group_status']
Run Code Online (Sandbox Code Playgroud) 我想在我的django项目中为我的所有组创建一个唯一的id,这将帮助我们轻松检测用户操作.
我尝试过以下方法:
楷模:
class Group1(models.Model):
urlhash = models.CharField(max_length=6, null=True, blank=True, unique=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True)
group_name = models.CharField(max_length=32)
Run Code Online (Sandbox Code Playgroud)
这是我给出的功能:
def save(self):
if not self.urlhash:
if self.user.profile.user_type == 'Business User':
self.urlhash = 'B' + str(self.user.id) + ('00') + str(self.id)
else:
self.urlhash = 'P' + str(self.user.id) + ('00') + str(self.id)
super(Group1, self).save()
Run Code Online (Sandbox Code Playgroud)
但是str(self.id)来了没有.
任何人都可以解释为什么会发生这种错误?
谢谢
根据@JPG和@sandip给出的解决方案,我终于得到了解决方案
def save(self):
super(Group1, self).save()
if not self.urlhash:
if self.user.profile.user_type == 'Business User':
self.urlhash = 'B' + str(self.user.id) + ('00') + str(self.id)
Group1.objects.filter(pk=self.pk).update(urlhash=self.urlhash)
else:
self.urlhash = …Run Code Online (Sandbox Code Playgroud) 我有一个人物模型。
class Person(models.Model):
name_first = models.CharField(max_length=100)
name_middle = models.CharField(max_length=100, null=True, blank=True)
name_last = models.CharField(max_length=100)
date_birth = models.DateField(null=True, blank=True)
date_death = models.DateField(null=True, blank=True)
Run Code Online (Sandbox Code Playgroud)
我正在尝试将其扩展到音乐界的不同角色:作曲家,表演者和赞助人。
一个人可以是一个,两个或所有三个角色。如果某人是表演者,我还需要为该人分配一种或多种乐器。在实例化时,您可能不知道某人是否是表演者。否则他们的角色会随着时间而改变。
我希望能够搜索并显示一个人(如果他是三个人)既是作曲家,钢琴家又是赞助人。例如:贝多芬是指挥,作曲家和钢琴家。
我对实现的最初想法是继承Person类。
class Composer(Person):
pass
class Performer(Person):
instrument = models.ManyToManyField(Instrument, verbose_name=_('instrument'), blank=True,)
class Patron(Person):
pass
class Instrument(models.Model):
name = models.CharField(max_length=100, null=True, blank=True)
Run Code Online (Sandbox Code Playgroud)
问题1:我应该改用某种抽象模型吗?如果是这样,我将如何处理?
问题2:我如何寻找一个人,并知道他们是否是作曲家,赞助人和/或表演者,以及他们是哪种表演者。
谢谢。