我有一个valid_till类型的属性DATE.
现在我想要做的是valid_till从今天开始比较.
$couponCheck = $this->Coupons->find('all', [
'conditions' => [
'coupon_code' => $couponCode
]
]);
if ($couponCheck->count() === 1) {
$coupon = $couponCheck->first();
if ($coupon->valid_till >= isToday()) {
echo 'Coupon is Valid';
} else {
echo 'Coupon is Expired';
}
} else {
echo 'This is not a valid coupon code';
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我怎么能这样做?
我已经使用pipenv内部/home/user/code/project目录设置了一个 python 项目。
我已设置PyCharm为该项目使用本地解释器
但是当我pipenv在其中运行命令时PyCharm's console会出现错误
$ pipenv
zsh: command not found: pipenv
Run Code Online (Sandbox Code Playgroud)
如何设置PyCharm使用pipenv项目中设置的环境?
我有一个应用程序notes和模型名称定义为
笔记/模型.py
from shorturls.models import ShortUrl
class Note(models.Model):
# columns here
def __str___(self):
return self.title
@receiver(post_save, sender=Note)
def post_save_note_receiver(sender, instance, created, *args, **kwargs):
if instance and created:
ShortUrl.objects.create(note=instance)
Run Code Online (Sandbox Code Playgroud)
和另一个应用程序shorturls来存储带有模型的每条记录的短 URL
短网址/models.py
from notes.models import Note
class ShortUrl(models.Model):
note = models.OneToOneField(Note, on_delete=models.CASCADE, blank=True)
short_key = models.CharField()
def __str__(self):
return self.short_key
@receiver(pre_save, sender=ShortUrl)
def pre_save_short_url_receiver(sender, instance, *args, **kwargs):
instance.short_key = unique_short_key_generator(instance)
Run Code Online (Sandbox Code Playgroud)
但是python manage.py makemigrations,它给出的错误为
File "path_to_app/shorturls/models.py", line 7, in <module>
from notes.models import Note
ImportError: cannot …Run Code Online (Sandbox Code Playgroud) 我正在使用Django 2.0并一直试图将用户重定向到其他视图get_context_data
我的网址模式是
mainapp.urls
urlpatterns = [
path('learn/', include('learn.urls', namespace='learn')),
path('admin/', admin.site.urls),
]
Run Code Online (Sandbox Code Playgroud)
应用程序网址
app_name = 'learn'
urlpatterns = [
path('success/<course_learn_id>/<session>', LearnSuccess.as_view(), name='success'),
]
Run Code Online (Sandbox Code Playgroud)
并LearnSuccess查看
class LearnQuestion(FormView):
form_class = SessionForm
template_name = 'learn/learn_question.html'
def get_context_data(self, **kwargs):
context = super(LearnQuestion, self).get_context_data(**kwargs)
course_learn = CourseLearn.objects.get(pk=self.kwargs['course_learn_id'])
session = self.request.GET['session']
question, question_type, options, complete = CourseLearn.objects.get_next_question(course_learn, session)
if complete:
return redirect('learn:success', course_learn_id=course_learn.pk, session=session)
context['complete'] = complete
context['question'] = question
context['question_type'] = context_type
context['options'] = options
context['session'] = session
return …Run Code Online (Sandbox Code Playgroud) 我正在使用Django 2.0,Django REST Framework和Django Filters来过滤查询集。
我已经安装django-filters并添加到INSTALLED_APPSas django_filters。
设置文件有
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'oauth2_provider.contrib.rest_framework.OAuth2Authentication'
),
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated'
],
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',)
}
Run Code Online (Sandbox Code Playgroud)
视图类就像
class AmountGivenViewSet(viewsets.ModelViewSet):
serializer_class = AmountGivenSerializer
permission_classes = (IsAuthenticated,)
filterset_fields = ('contact__id',)
def get_queryset(self):
queryset = AmountGiven.objects.filter(
contact__user=self.request.user
)
query = self.request.query_params.get('q', None)
if query:
queryset = queryset.filter(
Q(transaction_number=query) |
Q(comment__contains=query) |
Q(amountreturned__transaction_number=query) |
Q(amountreturned__comment__contains=query)
)
return queryset
Run Code Online (Sandbox Code Playgroud)
AmountGiven模型有一个联系的外键,因此想要根据contact_id进行过滤。 …
我正在使用 Anuglar 6
我有两个组件account和profile. account组件被添加到app-component。
我想通过单击account-component中的按钮打开profile -component。
但这给了我一个错误
Error: No component factory found for NgbModalBackdrop. Did you add it to @NgModule.entryComponents?
Run Code Online (Sandbox Code Playgroud)
该应用程序模块的样子
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent, HelloComponent, AccountComponent, ProfileComponent ],
entryComponents: [
ProfileComponent
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
我有一个python列表
[{'month': 8, 'total': 31600.0}, {'month': 9, 'total': 2000.0}]
Run Code Online (Sandbox Code Playgroud)
并希望像它一样生成它
[
{'month': 1, 'total': 0},
{'month': 2, 'total': 0},
...
{'month': 8, 'total': 31600},
{'month': 9, 'total': 2000},
...
{'month': 12, 'total': 0}
]
Run Code Online (Sandbox Code Playgroud)
为此,我正在对范围进行迭代 (1,13)
new_list = []
for i in range(1, 13):
# if i exists in month, append to new_list
# else add total: 0 and append to new_list
Run Code Online (Sandbox Code Playgroud)
我如何检查是否我的存在一个月,并得到字典?
我正在使用Django 2.2.x和DRF。
我有一个带有FileField的模型
file = models.FileField(upload_to=get_media_upload_path)
Run Code Online (Sandbox Code Playgroud)
文件正在上传,但在访问时obj.file,它给出了没有的 URLHTTPS
http://example.com/media/image.jpg
Run Code Online (Sandbox Code Playgroud)
我希望它是
https://example.com/media/image.png
Run Code Online (Sandbox Code Playgroud)
重定向已在nginx配置中设置。但我想要带有https.
设置
MEDIA_URL = '/media_/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn', 'media_root')
Run Code Online (Sandbox Code Playgroud) 我正在使用Django 2.2
我有许多与User模型反向相关的模型,我想从具有不同过滤器的每个模型中获取计数。
例如,我有一个Relations像这样的模型
status = (
('Active', 'active')
('Inactive', 'inactive')
)
class Relation(models.Model):
user = models.ForeignKey(User, related_name='relation')
status = models.CharField(choices=status, default=ACTIVE)
Run Code Online (Sandbox Code Playgroud)
现在我想为用户单独获取每个状态的计数和查询集。为此,我在User模型中定义了模型方法
def get_relation():
return self.relation.all()
def get_active_relation(self):
return self.relation().filter(status='active')
def get_inactive_relation():
return self.relation().filter(status='inactive')
def get_active_count():
return self.get_active_relation().count()
def get_inactive_count():
return self.get_inactive_relaiton().count()
Run Code Online (Sandbox Code Playgroud)
我的用户对象为
user = User.objects.prefetch_related(
'relation'
).get(pk=request.user.pk)
Run Code Online (Sandbox Code Playgroud)
现在,当我得到计数时,它会为此执行一个额外的查询
user.get_active_count()
Run Code Online (Sandbox Code Playgroud)
如何过滤对象prefetch_related?
我在另一个 SOF 答案中发现了从 prefetch_latedlambda获取值的用途: /sf/answers/882661811/max
是否也可以用于lambda过滤查询集?
我正在关注这个文档 https://confluence.atlassian.com/bitbucket/deploy-to-amazon-ecs-892623902.html 来设置一个管道来部署到 ECS 集群。
此文档使用自定义任务定义 JSON 文件,并在更新映像名称后将其用于部署。
我是否需要复制完整的任务定义 JSON 并将其放入我的存储库中?我的任务定义中有很多环境变量。我不想通过将它们放在存储库中来公开它们。
或者,任务定义模板将更新默认任务定义并创建新修订。(不覆盖)
部署步骤是
tags:
revision-*:
- step:
deployment: production
name: Deploy to ECS
script:
# Replace the docker image name in the task definition with the newly pushed image.
- export IMAGE_NAME=${ECR_USERNAME}/${BITBUCKET_REPO_SLUG}:latest
- envsubst < task-definition-template.json > task-definition.json
# Update the task definition.
- pipe: atlassian/aws-ecs-deploy:1.0.0
variables:
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
CLUSTER_NAME: $AWS_ECS_CLUSTER_NAME
SERVICE_NAME: $AWS_ECS_SERVICE_NAME
TASK_DEFINITION: 'task-definition.json'
Run Code Online (Sandbox Code Playgroud)
希望我的存储库中有一个定义文件 task-definition-template.json
如何使用预定义任务而不是使用 JSON 文件?另外,我在哪里可以找到有关管道的更多文档。
atlassian/aws-ecs-deploy
Run Code Online (Sandbox Code Playgroud) bitbucket amazon-web-services amazon-ecs bitbucket-pipelines
django ×4
python ×2
amazon-ecs ×1
angular ×1
bitbucket ×1
cakephp ×1
cakephp-3.0 ×1
datetime ×1
django-2.0 ×1
django-media ×1
ng-bootstrap ×1
pipenv ×1
pycharm ×1