model - How to make the user-oriented form in django? -
so, have model project, , has it's owner (user). every project has it's own filepathfield, depends on owner (different 'match' parameter of filepathfield different users). not model working principle.
i can rewrite constructor (__init__ function) , pass user there.
also can create factory create these models.
what serious people in case? (when must change model's fields' properties depending on user)
model:
class project(models.model): project_file = models.filepathfield(_("project file"), match='\.prj$', allow_files=true, allow_folders=false, recursive=true, path=projects_root, null=false) name = models.charfield(_("project name"), max_length=80, null=false) owner = models.foreignkey(user, verbose_name=_("owner"), null=false) something factory function (just workaround):
def getuserprojectform(data, user=none): class projectform(forms.modelform): def __init__(self, *args, **kwargs): forms.modelform.__init__(self, *args, **kwargs) try: user_folder = str(user.id) root_path = join(projects_root, str(user_folder)) except exception e: user_folder = none root_path = projects_root self.fields['project_file'].path = root_path self.filter_paths(user_folder) class meta: model = project fields = ['name', 'project_file'] def filter_paths(self, user_folder=none): import re if user_folder not none , str(user_folder) != '': directory = join(projects_root, str(user_folder)) else: directory = projects_root regexp = re.compile('^%s(.+)$' % directory) temp_choices = [] db_choices = project.objects.values_list('project_file', flat=true) choice in self.fields['project_file'].choices: cur_match = regexp.match(choice[0]) if cur_match: if choice[0] in db_choices: continue cur_choice = cur_match.groups()[0] print "debug" print choice[0], cur_choice, user_folder print "debug" temp_choices.append((choice[0], cur_choice.replace(separator, "", 1))) self.fields['project_file'].choices = temp_choices also. maybe knows how can exclude files in database? (the last cycle)
Comments
Post a Comment