Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

this is my model class UserProfile(models.Model): user = models.ForeignKey(User,…

  1. User

    this is my model class User_Profile(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) phone_number = models.IntegerField(null=True, blank=True) full_name = models.CharField(max_length=50, null=True, blank=True) project_number = models.CharField(max_length=20, null=True, blank=True) project_name = models.CharField(max_length=100, null=True, blank=True) sector_name = models.CharField(max_length=100, null=True, blank=True) contract_ordering = models.IntegerField(null=True, blank=True) objects = models.Manager() # default manager non_null_contract_ordering = User_Profile_Manager() # custom manager def str(self): return str(self.project_name) class Meta: verbose_name="profile" ordering = ['contract_ordering', 'project_name'] class User_ContractPermission(models.Model): choice = [ ('Yes', 'Yes'), ('No', 'No'), ] user = models.OneToOneField(User_Profile, on_delete=models.CASCADE) contract_app = models.CharField(max_length=250, choices=choice, null=True, blank=True) add_new_contract = models.CharField(max_length=250, choices=choice, null=True, blank=True) Sector_manager_action_2 = models.CharField(max_length=250, choices=choice, null=True, blank=True) Check_data_action_3 = models.CharField(max_length=250, choices=choice, null=True, blank=True) Cost_estimation_action_4 = models.CharField(max_length=250, choices=choice, null=True, blank=True) Financial_action_5 = models.CharField(max_length=250, choices=choice, null=True, blank=True) Copy_contract_action_6 = models.CharField(max_length=250, choices=choice, null=True, blank=True) Check_contract_action_7 = models.CharField(max_length=250, choices=choice, null=True, blank=True) TCTA_action_8 = models.CharField(max_length=250, choices=choice, null=True, blank=True) EVP_action_9 = models.CharField(max_length=250, choices=choice, null=True, blank=True) CEO_action_10 = models.CharField(max_length=250, choices=choice, null=True, blank=True) AssignAttach2_action_11 = models.CharField(max_length=250, choices=choice, null=True, blank=True) AssignAttach1_action_12 = models.CharField(max_length=250, choices=choice, null=True, blank=True) project_index = models.CharField(max_length=250, choices=choice, null=True, blank=True) sector_index = models.CharField(max_length=250, choices=choice, null=True, blank=True) main_index = models.CharField(max_length=250, choices=choice, null=True, blank=True) def str(self): return str(self.user) class Meta: verbose_name="User_ContractPermission" and i did that in views def get_user_data(request): user_profile = User_Profile.objects.get(user=request.user) user_tender_permission = User_TenderPermission.objects.get(user=request.user) user_contract_permission = User_ContractPermission.objects.get(user=user_profile) user_project_permission = User_ProjectPermission.objects.get(user=request.user) employee_profile = Employee_Profile.objects.get(user=request.user) user_OrgstructurePermission = User_OrgstructurePermission.objects.get(user=request.user) notification_to = Actions.objects.filter(Q(to_id=user_profile.id) & ~Q(is_read_cc=user_profile.id)) notification_cc = Actions.objects.filter(Q(cc=user_profile.id) & ~Q(is_read_cc=user_profile.id)) notification_count = notification_to.count() + notification_cc.count() circular_report = Circular.objects.filter(~Q(is_read_circular=user_profile.id) & ~Q(circular_no=None)) circular_notification_count = circular_report.count() return { 'user_tender_permission': user_tender_permission, 'user_contract_permission': user_contract_permission, 'user_project_permission': user_project_permission, 'user_profile': user_profile, 'user_OrgstructurePermission': user_OrgstructurePermission, 'employee_profile': employee_profile, 'circular_notification_count':circular_notification_count, 'notification_count':notification_count, }
    then i get this error get() returned more than one User_Profile -- it returned 3!

  2. Assistant

    The error you're encountering is because there are multiple instances of the "User_Profile" model that match the query filter. The ".get()" method expects only one instance to be returned, but in your case, there are three instances that match the filter criteria.

    To fix this issue, you can modify your query to use ".filter()" instead of ".get()", and then handle the multiple instances accordingly. For example:

    user_profiles = User_Profile.objects.filter(user=request.user)
    
    if user_profiles.exists():
        # Handle the multiple instances using a loop or by selecting a specific instance
        user_profile = user_profiles.first()  # Select the first instance
    else:
        # Handle the case when no instances match the filter
        user_profile = None
    

    By using ".filter()" instead of ".get()", you can get all the instances that match the filter criteria. In this case, we're selecting the first instance using the ".first()" method, but you can modify the logic as per your requirements.

    Make sure to update your view function accordingly.

  3. User

    ok Handle the multiple instances using a loop

  4. Assistant

    If you want to handle multiple instances of "User_Profile" by looping through them, you can modify the code as follows:

    user_profiles = User_Profile.objects.filter(user=request.user)
    
    for user_profile in user_profiles:
        # Perform actions for each user profile instance
        # For example, print the project name for each instance
        print(user_profile.project_name)
    

    In this modified code, the "User_Profile" instances that match the filter are stored in the "user_profiles" queryset. Then, you can loop through each instance using a for loop and perform the desired actions.

    Please note that if you have multiple instances of "User_Profile" that match the filter, you need to determine the specific action you want to perform for each instance. The above example simply prints the project name for each instance, but you can adjust the code as per your requirements.

  5. User

    based on the previoes code i need to show my all projects her by for loop {% if user_contract_permission.contract_app == 'Yes' %} {% if notification_count > 0 %} {% else %} {% if user_contract_permission.main_index == 'Yes' %}

                                            {% endif %}
    
                                            {% if user_contract_permission.project_index == 'Yes' %}
                                                <a href="{% url 'contract_project_index' %}">
                                            {% endif %}
    
                                            {% if user_contract_permission.sector_index == 'Yes' %}
                                                <a href="{% url 'contract_sector_index' %}">
                                            {% endif %}
    
                                            {% if user_contract_permission.Cost_estimation_action_4 == 'Yes' %}
                                                <a href="{% url 'contract_cost_estimation_index' %}">
                                            {% endif %}
                                        {% endif %}
                                                <div class="card">
                                                    <div class="card-body">
                                                        <div class="card-icon mb-4">
                                                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text">
                                                                <path d="M14 3v4a1 1 0 0 0 1 1h4"></path>
                                                                <path d="M14 3L9 8H5a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V5a2 2 0 0 0-2-2h-7"></path>
                                                                <path d="M9 8L9 15"></path>
                                                                <path d="M12 8L12 15"></path>
                                                                <path d="M15 8L15 15"></path>
                                                              </svg>
                                                        </div>
                                                        <h5 class="card-title mb-0" style="font-size: 120%; font-weight:1000; font-family: 'Cairo', sans-serif;">العقود <br>CONTRACTS
    
                                                        </h5>
                                                    </div>
                                                </div>
                                            </a>
    
                                    </div>
    
                                {% endif %}
    
  6. Assistant

    To show all projects using a for loop in your template, you can modify the code as follows:

    {% if user_contract_permission.contract_app == 'Yes' %}
        <!-- Other code -->
        {% if user_contract_permission.main_index == 'Yes' %}
            <a href="{% url 'contract_main_index' %}">
        {% endif %}
    
        {% if user_contract_permission.project_index == 'Yes' %}
            <a href="{% url 'contract_project_index' %}">
        {% endif %}
    
        {% if user_contract_permission.sector_index == 'Yes' %}
            <a href="{% url 'contract_sector_index' %}">
        {% endif %}
    
        {% if user_contract_permission.Cost_estimation_action_4 == 'Yes' %}
            <a href="{% url 'contract_cost_estimation_index' %}">
        {% endif %}
        
        <!-- Loop through all projects -->
        {% for project in user_contract_permission.user_profile.projects.all %}
            <!-- Display project details -->
            <div class="card">
                <div class="card-body">
                    <div class="card-icon mb-4">
                        <!-- Icon code -->
                    </div>
                    <h5 class="card-title mb-0" style="font-size: 120%; font-weight:1000; font-family: 'Cairo', sans-serif;">
                        {{ project.project_name }}
                    </h5>
                </div>
            </div>
        {% endfor %}
        <!-- Other code -->
    {% endif %}
    

    In this code, the {% for project in user_contract_permission.user_profile.projects.all %} loop iterates over all projects associated with the user profile. You can access the project details within the loop using project.project_name or any other project field.

    Make sure to replace {{ project.project_name }} with the appropriate project details you want to display.

    Note: The provided code assumes that there is a reverse relationship between User_Profile and Project models. If the relationship name is different or you're using a different structure, adjust the code accordingly.