简单的方法来随机化数组的条目使用STL?(easy way to randomize the entries of an array using stl?)
我可以使用stl来排序一个int *数组,像普通和简单一样
std::sort(myarray, myarray + size);
是否有任何平等的简单方法来随机化它?
谢谢
I can sort a int* array using stl, plain and simple like
std::sort(myarray, myarray + size);
Is there any equal simple way to randomize it?
thanks
原文:https://stackoverflow.com/questions/2208411
最满意答案
更有说服力的是,我的目标是使多对多医院 - 用户关系的管理成为双向的。 也就是说,在Hospitals的管理页面上,您将获得一个filter_horizontal用户,在用户管理页面上,您将获得一个filter_horizontal for Hospitals。
我放弃了与User建立关系的想法,而是使用UserProfile。 我最终使用了这张7岁的Django票底部提到的确切代码。
最后,我的代码是
#models.py class ReverseManyToManyField(models.ManyToManyField): pass try: import south except ImportError: pass else: from south.modelsinspector import add_ignored_fields add_ignored_fields([".*\.ReverseManyToManyField$",]) class Hospital(models.Model): name = models.CharField(max_length=200) authorized_users = models.ManyToManyField('UserProfile', blank=True) def __unicode__(self): return self.name class UserProfile(models.Model): user = models.OneToOneField(User) authorized_hospitals = ReverseManyToManyField(Hospital, through=Hospital.authorized_rads.through) def __unicode__(self): return self.user.username
和
#admin.py ##### Stuff to register UserProfile fields in the admin site class UserProfileInline(admin.StackedInline): model=models.UserProfile can_delete = False verbose_name_plural = 'profiles' filter_horizontal = ('authorized_hospitals',) class UserAdmin(UserAdmin): inlines = (UserProfileInline, ) class HospitalAdmin(admin.ModelAdmin): filter_horizontal = ('authorized_users', ) admin.site.unregister(User) admin.site.register(User, UserAdmin) ##### END UserProfile Stuff admin.site.register(models.Hospital, HospitalAdmin)
More eloquently stated, my goal was to make the administration of the many-to-many hospital-user relationship bidirectional. Which is to say, on the admin page for Hospitals, you'd get a filter_horizontal for users, and on the User admin page you'd get a filter_horizontal for Hospitals.
I ditched the idea of making the relationship with User, and instead made it with UserProfile. I ended up using the exact code mentioned at the bottom of this 7 year old standing Django ticket.
in the end, my code is
#models.py class ReverseManyToManyField(models.ManyToManyField): pass try: import south except ImportError: pass else: from south.modelsinspector import add_ignored_fields add_ignored_fields([".*\.ReverseManyToManyField$",]) class Hospital(models.Model): name = models.CharField(max_length=200) authorized_users = models.ManyToManyField('UserProfile', blank=True) def __unicode__(self): return self.name class UserProfile(models.Model): user = models.OneToOneField(User) authorized_hospitals = ReverseManyToManyField(Hospital, through=Hospital.authorized_rads.through) def __unicode__(self): return self.user.username
and
#admin.py ##### Stuff to register UserProfile fields in the admin site class UserProfileInline(admin.StackedInline): model=models.UserProfile can_delete = False verbose_name_plural = 'profiles' filter_horizontal = ('authorized_hospitals',) class UserAdmin(UserAdmin): inlines = (UserProfileInline, ) class HospitalAdmin(admin.ModelAdmin): filter_horizontal = ('authorized_users', ) admin.site.unregister(User) admin.site.register(User, UserAdmin) ##### END UserProfile Stuff admin.site.register(models.Hospital, HospitalAdmin)
相关问答
更多-
您需要在类似的范围中扩展SelectBox.redisplay函数: (function() { var oldRedisplay = SelectBox.redisplay; SelectBox.redisplay = function(id) { oldRedisplay.call(this, id); // do something }; })(); 确保在页面上初始化SelectBox后应用此选项,并且每次刷新选择框(选项移动,添加过滤器等) ...
-
在模型中存储对用户的引用。 models.py: from django.db import models from django.contrib.auth.models import User class MyModel(models.Model): user = models.ForeignKey(User) ... (your fields) ... 强制当前用户存储在该字段中(使用管理员时) 强制任何这些对象的列表(另外)由当前用户过滤(使用管理员时) 防止其他用户进行编辑(即使 ...
-
您无需编辑flatpages / admin.py。 相反,请创建一个从默认的FlatPageAdmin继承的FlatPageAdmin 。 您可能需要为以下admin.py文件创建一个customflatpage应用程序,或者您可能已经有了一个可添加到其中的实用程序应用程序。 #admin.py from django.contrib import admin from django.contrib.flatpages.models import FlatPage from django.contrib ...
-
Django自定义用户管理员登录(Django Custom User Admin Login)[2024-02-28]
我不确定这是否会有所帮助,但也许您可以使用灯具来加载初始数据: https : //docs.djangoproject.com/en/dev/howto/initial-data/ 然后,在创建模型时,将自动为第一个管理员填充1个值。 I'm not sure if this will help, but perhaps you can use fixtures to load initial data: https://docs.djangoproject.com/en/dev/howto/initia ... -
Django:在“用户更改”管理页面上显示filter_horizontal(Django: Show filter_horizontal on 'User Change' admin page)[2022-05-08]
更有说服力的是,我的目标是使多对多医院 - 用户关系的管理成为双向的。 也就是说,在Hospitals的管理页面上,您将获得一个filter_horizontal用户,在用户管理页面上,您将获得一个filter_horizontal for Hospitals。 我放弃了与User建立关系的想法,而是使用UserProfile。 我最终使用了这张7岁的Django票底部提到的确切代码。 最后,我的代码是 #models.py class ReverseManyToManyField(models.ManyT ... -
因此,无论它的价值如何,水平滤波器都不起作用,这对我来说很容易,所以我继续这样做: raw_id_fields = ('teams', 'players', ) 另外,请不要忘记在自定义模板中包含以下内容,以使此窗口小部件正常工作: So for whatever it's worth, the horizontal ...
-
更新!! 我们制作了一个扩展了默认django小部件的版本。 https://github.com/fabrique/django-sortedm2m 这似乎是做这项工作: http : //pypi.python.org/pypi? : action=display&name=django-sortedm2m 它只是呈现一个列表,所以它可以使用一些样式。 Update!! We made a version which extends the default django widget. https:// ...
-
你可以用.js或者用.css来做。 试试这个js: $(document).ready(function(){ $("#id_permissions_from").css("height", 600); $("#id_permissions_from").css("overflow", "scroll"); $("#id_permissions_to").css("height", 600); $("#id_permissions_to").css("over ...
-
在django-admin中,如何将filter_horizontal设置为默认值?(In django-admin, how can I set filter_horizontal as the default?)[2022-03-20]
修改预先存在的代码中定义的类的最佳方法是使用mixin 。 您需要修改ModelAdmin类的formfield_for_manytomany方法; 该方法在BaseModelAdmin定义 。 将以下代码添加到一个模块中,该模块保证在Django服务器启动时运行[您自己的某个应用程序的models.py ]: from django.contrib.admin.options import ModelAdmin from django.contrib.admin import widgets class ...