problem on the visibility of data entered by users with django

I designed my Django project, I established the relations of my database, all goes well, except that when a user enters data, other users can see the same data, especially form using FOREIGNKEY.

When a user connects the data entered by others, precisely the FOREIGNKEY, reappears on the form of other users.
from django.db import models
from django.contrib.auth.models import User

class Equipements(models.Model):
activite_compteur = [
(‘Actif’, ‘Actif’),
(‘Non_Actif’, ‘Non_Actif’),
choix_type = [
(‘prepayé’, ‘Prepayé’),
(‘postpayé’, ‘Postpayé’),
choix_phase = [

    ('Monophasique', 'Monophasique'),
    ('Diphasique', 'Diphasique'),
    ('Triphasique', 'Triphasique'),
choix_amperage = [
    (5, 5),
    (5, 10),
    (15, 15),
    (20, 20),
    (25, 25),
    (25, 30),

user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_equipment')

num_compt = models.CharField(verbose_name="Numero du Compteur", null=False, max_length=20, unique=True)
amperage_compt = models.IntegerField(verbose_name="Amperage", null=False, choices=choix_amperage, )
phase_compt = models.CharField(verbose_name="Phase du compteur", null=False, choices=choix_phase, max_length=25)
type_compt = models.CharField(verbose_name="Type de compteur", null=False, choices=choix_type, max_length=10)
localite_compt = models.CharField(verbose_name="localité", null=False, max_length=15)
# date_enregistrement = models.DateField(verbose_name='Date enregistrement', auto_now_add=True, null=False)
date_dinstallation = models.DateField(verbose_name="Date d'installation", null=False)
date_abonmt_compt = models.DateField(verbose_name="Date d'abonnement", null=False)
compt_actif = models.CharField(verbose_name="Activité du compteur", choices=activite_compteur, max_length=15)

client = models.ForeignKey(Client, on_delete=models.CASCADE)
# agent = models.ForeignKey(Agent, on_delete=models.CASCADE)
# direction = models.ForeignKey(Directions, on_delete=models.CASCADE)
emplacement_equip = models.OneToOneField(Emplacement_equip, on_delete=models.CASCADE)

def __str__(self):
    return f'{self.num_compt}'

def forms(request):
if request.method == ‘POST’:
form = EquipementsForm(request.POST)
if form.is_valid():
equipements =
equipements.user = request.user
return redirect(‘forms’)
form2 = EquipementsForm()
Equipement_user = Equipements.objects.filter(user=request.user)

context = {
    'form2': form2,
    'Equipement_user': Equipement_user
return render(request, 'forms.html', context)

class EquipementsForm(forms.ModelForm):
class Meta:
model = Equipements
fields = [‘num_compt’, ‘amperage_compt’, ‘phase_compt’, ‘type_compt’, ‘localite_compt’, ‘date_dinstallation’,
‘date_abonmt_compt’, ‘compt_actif’, ‘client’, ‘emplacement_equip’]

Side note: When you post code here, please enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (You don’t need to repost your code, edit your post to put lines of ``` before and after each file of code. The ``` must be lines by themselves and not part of any other text or code.)

You’ve described the current situation, but what is it that you’re looking to do here? What exactly is it that you would like to have happen?

On my django application, I have input forms, but the existing relations between the entities, make that when a user enters and saves the data, the data the foreign keys of the tables entered by the previous user reappear.

I don’t want the data entered and saved in the database by one user to not reappear when another user logs in.

Got it - that makes a lot of sense.

So, in general, this means that you’re going to want to add a filter to your various queries to only retreive data belonging to a particular user.

If you’re looking for more specific assistance, please fix the formatting of the code in your post as described in my previous response, it’ll be a lot easier to discuss this that way.

You must pass (user_id) as a parameter in your view function to prevent this behavior i.e
When you use ( request.user) in the query that is mean the user who is already logged in and create the record in the database , But when another user logged in you must make like

def foo(request, user_id):
    qs = your_model.objects.get(user_id= user_id)

Here the request.user can’t open or view the data entered by another user.
By checking if the(user_id == request.user) then the logged in user can view the data that he entered, otherwise redirect him to another page or to 403 page or anything you want.

Also you must protect your function like the above from changing the passed id from editing it manually ( I mean: users (logged in users can change and edit the id in the “URL” itself))
So you must use a decorator for this foo function.

Hope it helps