django - Many to many - proper usage -
i have models this:
user: #somefields apps = models.manytomanyfield(app)
here, user created during registration. links apps using many many in separate page user selects(multiple) apps, no other fields.
what best way populate apps in django ?
for example, if can create user also, can use modelform .save(commit=false) create user , .save_m2m() link apps. here dont need create user. having user already, need add apps. how ?
newly added details: know how normal "form" , normal web way. want write quality code want know. if there nothing wrong in using normal form fine.
there multiple ways it, example:
class userappform(modelform): class meta: model = user fields = ['apps']
and in view, configure routing can:
- get id of user select
- raise error if not find id (instead of creating new one)
- initialize form user
for example:
def user_add_app(request, pk): user = get_object_or_404(user, pk=pk) if request.method == "post": form = userappform(request.post, instance=user) if form.is_valid(): user = form.save() return httpresponseredirect(<some redirect>) else: form = employeeform(instance=user) return render(request, "<some html file>", {'form': form})
Comments
Post a Comment