English 中文(简体)
我怎样才能查一查 贾尼戈的用户群 是否有变化?
原标题:How can I check if the groups of a user in django changed?

我把一个断点放进我的控制器里 用的是:

def handle_user_save(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)
        EmailNotifications.objects.create(user=instance)
    else:
        import pdb;pdb.set_trace()

post_save.connect(handler_user_save, sender=User)

在 pdb 内 :

(Pdb) dir(instance)
[ DoesNotExist ,  MultipleObjectsReturned ,  __class__ ,  __delattr__ ,  __dict__ ,  __doc__ ,  __eq__ ,  __format__ ,  __getattribute__ ,  __hash__ ,  __init__ ,  __metaclass__ ,  __module__ ,  __ne__ ,  __new__ ,  __reduce__ ,  __reduce_ex__ ,  __repr__ ,  __setattr__ ,  __sizeof__ ,  __str__ ,  __subclasshook__ ,  __unicode__ ,  __weakref__ ,  _base_manager ,  _default_manager ,  _deferred ,  _get_FIELD_display ,  _get_next_or_previous_by_FIELD ,  _get_next_or_previous_in_order ,  _get_pk_val ,  _get_unique_checks ,  _meta ,  _perform_date_checks ,  _perform_unique_checks ,  _set_pk_val ,  _state ,  addressbookentry_set ,  blogcomment_set ,  blogentry_set ,  check_password ,  clean ,  clean_fields ,  date_error_message ,  date_joined ,  delete ,  email ,  email_user ,  emailnotifications ,  first_name ,  full_clean ,  get_absolute_url ,  get_all_permissions ,  get_full_name ,  get_group_permissions ,  get_next_by_date_joined ,  get_next_by_last_login ,  get_previous_by_date_joined ,  get_previous_by_last_login ,  get_profile ,  groups ,  has_module_perms ,  has_perm ,  has_perms ,  has_usable_password ,  id ,  is_active ,  is_anonymous ,  is_authenticated ,  is_staff ,  is_superuser ,  last_login ,  last_name ,  logentry_set ,  natural_key ,  objects ,  password ,  pk ,  prepare_database_save ,  project_set ,  save ,  save_base ,  serializable_value ,  set_password ,  set_unusable_password ,  unique_error_message ,  user_permissions ,  username ,  userprofile ,  validate_unique ,  vote_set ]

并且:

(Pdb) instance.groups.all()
[]

它似乎没有显示此点的更改 。 但最有可能的情况是这样, 因为我正在进行 db 查询以获取分组 。 还有其它地方我可以截取用户模式的更新吗?

或许其中的一个例子能给我答案呢?这些干净的方法/领域似乎都没有任何有用之处。

最佳回答

由于 User Group 之间的关系是 ManyToManyField ,因此对这种关系的修改触发了另一个信号: m2m_changed Document :

当模型实例中修改 manyTony 字段时发送。 严格地说, 这不是一个模型信号, 因为它是由 manyTony Field 发送的, 而是因为它补充了预保存/ post_ save 和 预删除/ post_ delete / post_ delete 在跟踪模型变化时, 它包含在这里 。

因此,您可以检查特定用户组是否随此变化:

def handle_m2m_changed(sender, instance, action, **kwargs):
    if action ==  post_add  or action ==  post_remove :
        print(instance.groups.all())

m2m_changed.connect(handle_m2m_changed, sender=User.groups.through)
问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签