本文整理汇总了Python中django.contrib.auth.models.User.objects方法的典型用法代码示例。如果您正苦于以下问题:Python User.objects方法的具体用法?Python User.objects怎么用?Python User.objects使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.auth.models.User
的用法示例。
在下文中一共展示了User.objects方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: signup
# 需要导入模块: from django.contrib.auth.models import User [as 别名]
# 或者: from django.contrib.auth.models.User import objects [as 别名]
def signup(request):
if not settings.REGISTRATION_OPEN:
return HttpResponseForbidden()
ctx = {}
form = forms.AvailableEmailForm(request.POST)
if form.is_valid():
email = form.cleaned_data["identity"]
user = _make_user(email)
profile = Profile.objects.for_user(user)
profile.send_instant_login_link()
ctx["created"] = True
else:
ctx = {"form": form}
response = render(request, "accounts/signup_result.html", ctx)
if ctx.get("created"):
response.set_cookie("auto-login", "1", max_age=300, httponly=True)
return response
示例2: get
# 需要导入模块: from django.contrib.auth.models import User [as 别名]
# 或者: from django.contrib.auth.models.User import objects [as 别名]
def get(self, request, *args, **kwargs):
results = {}
for election in Election.objects.filter(current=True).order_by('id'):
results[election.slug] = {
'election_date': text_type(election.election_date),
'name': election.name,
'url': reverse('election-detail', kwargs={
'version': 'v0.9',
'slug': election.slug})
}
res = HttpResponse(
json.dumps(results), content_type='application/json',
)
res['Expires'] = date.today() + timedelta(days=7)
return res
示例3: test_regression_7957
# 需要导入模块: from django.contrib.auth.models import User [as 别名]
# 或者: from django.contrib.auth.models.User import objects [as 别名]
def test_regression_7957(self):
"""
Regression test for #7957: Combining extra() calls should leave the
corresponding parameters associated with the right extra() bit. I.e.
internal dictionary must remain sorted.
"""
self.assertEqual(
(User.objects
.extra(select={"alpha": "%s"}, select_params=(1,))
.extra(select={"beta": "%s"}, select_params=(2,))[0].alpha),
1
)
self.assertEqual(
(User.objects
.extra(select={"beta": "%s"}, select_params=(1,))
.extra(select={"alpha": "%s"}, select_params=(2,))[0].alpha),
2
)
示例4: test_regression_8819
# 需要导入模块: from django.contrib.auth.models import User [as 别名]
# 或者: from django.contrib.auth.models.User import objects [as 别名]
def test_regression_8819(self):
"""
Regression test for #8819: Fields in the extra(select=...) list
should be available to extra(order_by=...).
"""
self.assertQuerysetEqual(
User.objects.filter(pk=self.u.id).extra(select={'extra_field': 1}).distinct(),
['<User: fred>']
)
self.assertQuerysetEqual(
User.objects.filter(pk=self.u.id).extra(select={'extra_field': 1}, order_by=['extra_field']),
['<User: fred>']
)
self.assertQuerysetEqual(
User.objects.filter(pk=self.u.id).extra(select={'extra_field': 1}, order_by=['extra_field']).distinct(),
['<User: fred>']
)
示例5: __init__
# 需要导入模块: from django.contrib.auth.models import User [as 别名]
# 或者: from django.contrib.auth.models.User import objects [as 别名]
def __init__(
self, data=None, instance=None, request=None, *args, **kwargs
):
if data is None:
data = {}
else:
data = data.copy()
# If this is a new IPRange, fill in the 'user' and 'subnet' fields
# automatically, if necessary.
if instance is None:
start_ip = data.get("start_ip")
subnet = data.get("subnet")
if subnet is None and start_ip is not None:
subnet = Subnet.objects.get_best_subnet_for_ip(start_ip)
if subnet is not None:
data["subnet"] = subnet.id
if request is not None:
data["user"] = request.user.username
elif instance.user and "user" not in data:
data["user"] = instance.user.username
super().__init__(data=data, instance=instance, *args, **kwargs)
示例6: assisted_merge
# 需要导入模块: from django.contrib.auth.models import User [as 别名]
# 或者: from django.contrib.auth.models.User import objects [as 别名]
def assisted_merge(modeladmin, request, queryset):
GearAssistedMerge.objects.all().delete()
if queryset.count() > 1:
return
orphans = queryset.filter(master=None)
for orphan in orphans:
matches = difflib.get_close_matches(orphan.name, [x.name for x in queryset], cutoff=0.6)
for match in matches:
slaves = Gear.objects.filter(name=match).exclude(pk=orphan.pk)
for slave in slaves:
# The following line needs some explaining:
# With the first Q(), I exclude mutual master/slave
# relationship (like a -> b and b -> a).
# With the second Q(), I exclude dependencies that generate
# a tree deeper than 2 level (a -> b -> c).
if not GearAssistedMerge.objects.filter(Q(slave=orphan) | Q(master=slave)):
s = difflib.SequenceMatcher(None, orphan.name, match)
merge, created = GearAssistedMerge.objects.get_or_create(master=orphan, slave=slave)
merge.cutoff = s.quick_ratio()
merge.save()
return HttpResponseRedirect('/admin/astrobin/gearassistedmerge/')
示例7: soft_merge
# 需要导入模块: from django.contrib.auth.models import User [as 别名]
# 或者: from django.contrib.auth.models.User import objects [as 别名]
def soft_merge(modeladmin, request, queryset):
masters = [x.master for x in queryset]
if not all(x == masters[0] for x in masters):
# They're not all the same!
return
master = masters[0]
slaves = [x.slave for x in queryset if x != master]
for slave in slaves:
# These are all the items that are slave to this slave.
slaves_slaves = Gear.objects.filter(master=slave)
if slave.master:
slave.master.master = master
slave.master.master.save()
for slaves_slave in slaves_slaves:
slaves_slave.master = master
slaves_slave.save()
slave.master = master
slave.save()
示例8: submit_premium_offer_discount
# 需要导入模块: from django.contrib.auth.models import User [as 别名]
# 或者: from django.contrib.auth.models.User import objects [as 别名]
def submit_premium_offer_discount(self, request, obj):
profiles = UserProfile.objects \
.exclude(premium_offer=None) \
.exclude(premium_offer_expiration=None) \
.exclude(premium_offer_expiration__lt=datetime.now()) \
.filter(receive_marketing_and_commercial_material=True) \
.filter(
Q(premium_offer_sent=None) |
Q(premium_offer_sent__lt=datetime.now() - timedelta(days=30))
)
profiles = [x for x in profiles if premium_get_valid_usersubscription(x.user) is None]
recipients = UserProfile.objects.filter(pk__in=[x.pk for x in profiles])
self.submit_email(request, obj, recipients.values_list("user__email", flat=True))
recipients.update(premium_offer_sent=datetime.now())
示例9: _get_default_issue_fields
# 需要导入模块: from django.contrib.auth.models import User [as 别名]
# 或者: from django.contrib.auth.models.User import objects [as 别名]
def _get_default_issue_fields(self):
rb_pk = self._DEFAULT_RB_ISSUE_FIELDS_PKS if self._course_rb_integrated else set()
contest_pk = self._DEFAULT_CNTST_ISSUE_FIELDS_PKS if self._course_contest_integrated else set()
default_issue_fields_pks = self._DEFAULT_ISSUE_FIELDS_PKS | rb_pk | contest_pk
default_issue_fields = set(IssueField.objects.filter(pk__in=default_issue_fields_pks))
return default_issue_fields_pks, default_issue_fields
示例10: get_deleted_issue_fields
# 需要导入模块: from django.contrib.auth.models import User [as 别名]
# 或者: from django.contrib.auth.models.User import objects [as 别名]
def get_deleted_issue_fields(self):
return set(IssueField.objects.filter(pk__in=self.get_deleted_pks()))
示例11: add_group_with_extern
# 需要导入模块: from django.contrib.auth.models import User [as 别名]
# 或者: from django.contrib.auth.models.User import objects [as 别名]
def add_group_with_extern(self):
if self.group_with_extern is None and self.can_be_chosen_by_extern:
group, ok = Group.objects.get_or_create(year=self.year, name=u'%s - слушатели' % self.name)
group.save()
self.group_with_extern = group
self.groups.add(group)
self.save()
示例12: get_students
# 需要导入模块: from django.contrib.auth.models import User [as 别名]
# 或者: from django.contrib.auth.models.User import objects [as 别名]
def get_students(self):
return User.objects\
.filter(group__in=self.groups.all())\
.distinct()\
.order_by('last_name', 'first_name')
示例13: get_default_teacher
# 需要导入模块: from django.contrib.auth.models import User [as 别名]
# 或者: from django.contrib.auth.models.User import objects [as 别名]
def get_default_teacher(self, group):
try:
return DefaultTeacher.objects.filter(course=self).get(group=group).teacher
except DefaultTeacher.DoesNotExist:
return None
示例14: handle
# 需要导入模块: from django.contrib.auth.models import User [as 别名]
# 或者: from django.contrib.auth.models.User import objects [as 别名]
def handle(self, *args, force, **options):
users = User.objects.filter(email__in=
User.objects
.exclude(
is_active=False,
date_joined__lt=now() - timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS),
)
.values('email')
.annotate(count=models.Count('*'))
.filter(count__gt=1)
.values('email')
).order_by('email', 'date_joined')
deletions_by_model = Counter()
with self.get_mail_backend(force) as connection:
for email, group in itertools.groupby(users, key=operator.attrgetter('email')):
try:
with transaction.atomic():
deletions_by_model += self.dedup(email, group, connection)
if not force:
raise Exception('skipped in dry-run mode')
except Exception as e:
self.stdout.write(' *** {}: {}'.format(email, e))
if deletions_by_model:
self.stdout.write('')
self.stdout.write('users deleted by model:')
for model, count in deletions_by_model.most_common():
self.stdout.write('{}: {}'.format(self.format_model(model), count))
if not force:
self.stdout.write('')
self.stdout.write(' *** Nothing done in dry-run mode!')
self.stdout.write(' *** Run again with --force to commit these changes.')
示例15: _make_user
# 需要导入模块: from django.contrib.auth.models import User [as 别名]
# 或者: from django.contrib.auth.models.User import objects [as 别名]
def _make_user(email, with_project=True):
username = str(uuid.uuid4())[:30]
user = User(username=username, email=email)
user.set_unusable_password()
user.save()
project = None
if with_project:
project = Project(owner=user)
project.badge_key = user.username
project.save()
check = Check(project=project)
check.name = "My First Check"
check.save()
channel = Channel(project=project)
channel.kind = "email"
channel.value = email
channel.email_verified = True
channel.save()
channel.checks.add(check)
# Ensure a profile gets created
Profile.objects.for_user(user)
return user