facebook django type select for many-to-many fields - django

I am wondering if there is maybe a way with django to display bring in extra data into the display of CheckboxSelectMultiple ie avatar or first_name or last_name
With the manytomany field its really good that a user can check their options and proceed, but what sometimes it might be that a user has the same information twice, ie a name that is the same, or a reference to something that is the same.
Facebook has a very nice style when you want to share something with somebody you can select that user and see their avatar at the same time, so if you have two friends with the same name and surname, you can always distinguish them by their avatar.
is there some way with the __init__ that this can be achieved, or do you guys have any pointers on achieving this in another way?

The answer linked to below has an example of a custom SelectMultiple widget which makes the objects being selected available for use in presenting further details to the user in the widget's render() method:
Formsets with checkboxes

Related

Is it possible in django model form to show certain fields if user select a related field?

I have a user model field with choices asking about employment, if person select the choice employed then only I want to show fields asking about employer details in user inerface, any possibilities? Thank you in advance.
django's native forms do not, by default, have this ability. this is generally accomplished by adding the appropriate client-side controls into the form's template; e.g. using javascript to show/hide some fields based on another field's value. this is commonly accomplished using javascript or in some cases even pure css.
if you're dealing with a multi-step form across multiple pages or if you know in advance what the value that might include/remove fields is, you can override the form's __init__ method to manipulate its self.fields property to add or remove fields as desired when the form is initially being generated.
good luck!

implementing multiple profiles with django-all-auth

Django noob here - I was recently pointed to django-all-auth for registering/handling users, which I'm finding awesome in its near instant setup.
However I'm stumbling at trying to implement multiple user profile models. In reading other answers I've found this to be the closest answer thus far, but not really what I need.
When coding my own basic registration the user would select an account type (basic, pro, elite for example - each being their own profile model). Depending on the link selected the signup form would display both the generic User registration form as well as the profile form of the type chosen by the user.
I know I can go so far as to completely customize all-auth and make something like this work, but I'm hoping to be pointed in a direction that involves less wrecking of the original app. I've thought about having user redirected after signup to choose a profile type, but that seems to be a lot of extra steps.
Thanks for the help!
To extend the basic user class, just subclass AbstractUser. You can find that in the docs. With this you can add the fields your basic user is missing.
Now, you want several types of profiles with different fields, or perhaps the same fields but adding new fields every time.
You can create something like:
class ProfileBase(models.Model):
user=models.OneToOneField(User)
class ProfilePro(ProfileBase):
pro_field=models.SomeField(Foo)
#You can extend ProfilePro here if you want it to include the pro_field
class ProfileElite(ProfileBase):
elite_field=models.someField(Bar)
Once you have these models creating the forms should be easy.
Be aware, when you subclass this way django creates 1 table per model, including on the subclass table only the new fields. This makes necessary a join per level of inheritance so try not to abuse that.
There is a second way to use inheritance:
class ProfileBase(models.Model):
user=models.OneToOneField(User)
class Meta:
abstract=True
If you define the base class as abstract you won't have a table for it, so you can't create objects for that model, but each of your subclasses will be on it's own table. If you do it this way you may need extra logic to handle cases when user changes of type of profile (i.e. went from pro to elite).
To keep this separated from django-allauth, just finish the registration (create a form and in your settings define ACCOUNT_SIGNUP_FORM_CLASS to override all-auth default with your basic info + pick a profile type form) and once your user is logged in, redirect them to their profile to finish the process.

How can I use model like a custom field

Recently I've been developing a Django website, which includes the owner being able to add content with descriptions etc.
The problem I'm having is: How can I make the fields support multiple languages? (3 in this case)
The approach I tried was: Creating a model with 3 text fields, have my content model take that model as a foreign key. This sort of works, but now I would have to create all the descriptions first, separately, before creating the actual object it is being used by. This is, in my opinion, a bad idea.
What I would like to be able to do, is to have 3 text fields in the model which is actually using those 3 text fields' admin page, but without actually having 3 text fields in that model.
Using inlines would work, but I'd have to make my multilanguage textfield model have a foreign key to my content model, instead of the other way. This would mean the multilanguage model works for only other model type.
So, to clear the question up:
How can I have a TextField and a CharField support multiple languages?
How can I show the ForeignKey's target model's creation widget in it's owner's admin page?
How can I use inlines, without locking the inline to just one model type?
How can I make a model act like a field?
How can I write a custom TextField?
Answering any of those will be enough for me to solve my problem.
Thanks.
There is too many questions and the docs is at your reach... I'll just answer the easiest one you should have search for by yourself.
How can I have a TextField and a CharField support multiple languages?
You should have a look to i18n here
How can I write a custom TextField?
Have a look to custom Fields

Django: "reverse" many-to-many relationships on forms

The easiest example of the sort of relationship I'm talking about is that between Django's Users and Groups. The User table has a ManyToMany field as part of its definition and the Group table is the "reverse" side.
A note about my constraints: I am not working with the Admin interface at all nor is that an option.
Now, onto the programming problem. I need to write a form that is used to edit MyGroup instances, defined simply as the following:
class MyGroup( Group ):
some_field = models.CharField( max_length=50 )
I want to be able to have a form page where I can edit both some_field and which users are members of the group. Because I'm working with a model, a ModelForm seems obvious. But I cannot figure out how to get Django to include the users because it is on the reverse side of the User-Group relationship. Ideally, I'd like the display widget for specifying the users to be like the one for specifying permissions that is found on the User and Group pages within Admin.
inline-formsets
do the trick for a foreign key relationship.
GroupUserInlineFormSet = inlineformset_factory(MyGroup, User, form=PurchaseOrderEditForm, max_num=100, extra=2, can_delete=False)
guformset = GroupUserInlineFormSet (instance=mygroup)
might point you in the right direction. not sure how this can work with a manytomany relationship.
I never found a great way to do this. I ended up writing custom forms that manage creating the fields and specifying an appropriate queryset on them.
For the display portion of the question, there is a way to use the SelectFilter (a.k.a. horizontal filter) on regular pages. One page with instructions I found is here, and there was another that was helpful, but I can't seem to re-located it.
I'm considering writing up a more thorough guide to both parts of this process. If anyone is interested please let me know, it'll give me the push to actually get it done.

Django app with selectable field types from admin panel?

Looking for some implementation ideas here. I am trying to design a custom polling system for my school, to allow teachers to give students polls to take.
I have a Poll model, Question model (with a foreignkey to Poll model), and Choice model (with a foreignkey to the Question model).
What I need to be able to do is allow whoever is adding questions to choose the type of choices that will be shown. For example, one question should be able to be multiple choice (displayed via a radio buttons), and another question should be able to be "Check all that apply."
What is the best way to allow the creator of the poll to determine how the choices are shown? Should I do a CharField() with choices, and deal with it manually in the view? That doesn't seem efficient.
Use a field in your model that has choices for the different ways people can choose (it doesn't have to be CharField, you could also use a SmallIntegerField and map numbers). Create different form classes for each way of choosing and decide in the view which one to apply based on the value of the "way of choosing"-field. It's a straight-forward way and not that much of a hassle if abstracted nicely.