How can I create a Drupal 8 view that includes all users with the same field value as the logged in user - drupal-8

Drupal 8. The fields are in a Profile Type, not User.
Scenario: User A color = blue, User B color = blue, User C color = red, User D color = red.
User A should see Users A and B in their view, and User D should see users C and D in their view.

Related

Django admin update of form fields based on foreign key model selection

In django admin I have Model A with a foreign key association to Model B. Model B's values change based on the value of Model A.
When a Model B object is selected for association with a Model A object, I would like to immediately display updated values for Model B based on the current value of Model A.
I know that I can override the on_save method in the form to update the values when the user saves the form to the database. However, I would like the admin view to display the values before the user hits save.
What do I need to hook into to make this update happen?
Thank You
If you want to dinamically filter Model B values in the changeview during user interaction (that is: before submission), you need javascript:
1) after page rendering, attach a "change handler" to Model A input field
2) in that handler, call via Ajax a view to retrieve the list of values available for Model B according to the currently selected value of Model A
3) upon receiving the list, update the Model B input field accordingly
4) also, after the initial page rendering you should explicitly invoke the handler in order to have Model B input field correctly initialized
This should work for both "add" and "change" view.
I do believe that a very detailed tutorial on how to implement this procedure can be found here:
https://simpleisbetterthancomplex.com/tutorial/2018/01/29/how-to-implement-dependent-or-chained-dropdown-list-with-django.html
The example refers to a front-end view, but can be easily adapted to the admin changeview
Let's say here what you've got for models:
# Model B
class ModelB(models.Model):
pass
# Model A
class ModelA(models.Model):
b_link = models.ForeignKey(ModelB)
I assume that you don't want to use javascript to manipulate the form but parsing it from server. In that case, what you can do is just create a preview Model B, and create the ModelForm from this model.
For example:
class ModelB(models.Model):
...
# add a method to preview B - This will not save model
def preview_b(model_a):
# update values of b according to model_a
b.derived_value = model_a.value
# file: forms.py
class ModelBForm(ModelForm):
class Meta:
model = ModelB
# file: views.py
b_model = Model.objects.all().first()
b_model.preview_b(a_model)
form = ModelBForm(instance=b_model)
This, of course, requires you to post back to server whenever choosing a new ModelA but I think that was what you wanted.

How to render a ForeignKey field as CharField in Django forms

My Model
class Collaborator(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
My Form:
class CollaboratorForm(forms.ModelForm):
user = forms.CharField(max_length=30, required=False)
class Meta:
model = Collaborator
fields = ('user',)
The template render the user input text as an autocomplete field loaded from User model.
The page shows the user input text field correctly, however when I go to edit a Collaborator the user input text field shows the user Id and I want to show the username instead.
You want to figure out first what the exact behavior you want is.
Let's say I'm user with id=1, username="Joel". When you produce your CollaboratorForm and it shows "Joel" (my username) on the form and you edit it to read "Jane", are you:
trying to edit my name to "Jane", so that id=1 now has a username=Jane ?
or trying to change the associated user to the user matching that username?
Once you know that, you can proceed.
I'd change the user = forms.CharField(..) to username = forms.CharField(..); that's what your really showing, after all.
In your view, when you create your form, pass it the initial values of {'username':username-of-user-that-is-currently-related}.
In your view, when you process the form, before you save it, use the cleaned_data['username'] to either (a) update the username of that user (if the first case above) or (b) get the ID of the new user and save that to the .user field (if the second case above).
Of course, if what you want is really at a higher-level "let people pick the user based on the username", you might be able to solve this differently and more easily--by not using a CharField at all, and using a ChoiceField instead, so you could show a drop down menu of users with the key=ID and value=name. Depending on how many users you have, though, that might not scale well.

How to check if notification has been viewed like in facebook?

Whenever there are facebook notifications a red indicator appears until the notifications have been viewed for the first time.
How could I implement something similar on my site (preferably with django if anything needs to be done server-side)?
The simplest approach would be:
Have a field in the models for viewed_at which is nullable. Once viewed, set the date. If no value is set, that means the user has not viewed it yet.
If there are multiple users whom this feature needs to be extended to, have a foreign key reference:
class MyViewableObject(models.Model):
#fields
class MyViewableViewedBy(models.Model):
user = models.ForeignKey(User)
viewable = models.ForeignKey(MyViewableObject)
Now, when MyViewableObject is viewed, in the view method, create an association for the request.user in the MyViewableViewedBy model.
The user has not viewed if there is no corresponding user entry for the object in the model MyViewableViewedBy

django models selecting users satisfying a criteria and taking intersection

I have the following two database model:
class UserProfile(models.Model):
description = models.TextField()
class Channel(models.Model):
subscribed = models.ManyToManyField(UserProfile,
related_name="subscribed")
Given, a channel I want to display the users subscribed to the channel in one column and the ones who are not subscribed to the channel in other column.
Getting the users who are subscribed to a channel is no issues. How do I select the users who are not subscribed to the channel and the send the user profile objects of users who are not subscribed.
Just use exclude instead of filter.
c = Channel.objects.get(pk=1)
subscribed = c.subscribed_set.all()
not_subscribed = UserProfile.objects.exclude(pk__in=[s.pk for s in subscribed])

Repeat fields in form

I'm writing an application where users can upload a maximum of 3 images to the application and was wondering if it was possible to have the same field repeated. My model looks like this:
class Image(models.Model):
emailAddress = models.EmailField(max_length=75)
image = ImageField(upload_to='photos')
caption = models.CharField(max_length=100)
Where when the image changes only the caption and image change, so only one email field would be needed. I have a form that uses my model to create the form. Would I have to create a static form then read all the data in from the HTTP request?