Create Parent Class object from child class's ModelForm

blueChair

I have a Model 'Appointment'. In that class patient is associated with built-in User model.I have made a modelform for that class. Is it possible to instantiate an object of another class (User in field patient) from inside the same form. I am not able to access User Model's field in the form. How can this be done ?

class Appointment(models.Model):
    doctor = models.ForeignKey(Doctor)
    clinic = models.ForeignKey(Clinic, null=True, blank=True)
    hospital = models.ForeignKey(Hospital, null=True, blank=True)
    day = models.DateField()
    time = models.TimeField()
    patient = models.ForeignKey(User)

I have created a ModelForm for this model as below:

class HospitalCreateAppointmentForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(HospitalCreateAppointmentForm, self).__init__(*args, **kwargs)
        for field in iter(self.fields):
                self.fields[field].widget.attrs.update({
                    'class': 'form-control'
                })

    class Meta:
        model = Appointment
        fields = ['doctor','hospital','day','time','patient']
        widgets = {
            'day': forms.SelectDateWidget(),
        }
Krishna Sunuwar

I am not sure purpose behind instantiating user object in HospitalCreateAppointmentForm. However, just in case you really need, you have to import User model in form file. For example if your HospitalCreateAppointmentFor is in forms.py, import User module like

from django.contrib.auth.models import User

And you can access field like:

User._meta.fields

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I pass a parent id as an fk to child object's ModelForm using generic class-based views in Django?

From Dev

Typecasting an object from parent class to child

From Dev

Can I create object of child class using method from parent class

From Dev

Can I create object of child class using method from parent class

From Dev

Calling method of child class from object of parent class in C++

From Dev

public object from parent class is null in child class xaml

From Dev

Why is it necessary for parent class to have a default constructor if we do not create child class object in child class?

From Dev

Assigning a child class object to an object of the parent class

From Dev

Assigning a child class object to an object of the parent class

From Java

How to call a Parent Class's method from Child Class in Python?

From Dev

how to simply access property of parent class from child class by making object of child class

From Dev

Create a child instance from parent class in AngularJS+Typescript

From Dev

How to check if child object is from specific class (and not parent) (Java)

From Dev

Get child class from parent

From Dev

Appending values from a child class to parent class

From Dev

Return child class from parent class

From Dev

Accessing child class prototype from parent class

From Dev

Use child's inner class from parent template

From Dev

Return child class from parent's static method

From Dev

How to use parent project's class from a child project

From Dev

Why is it allowed for a parent class object to be instantiated using a child class's constructor?

From Dev

Why is it allowed for a parent class object to be instantiated using a child class's constructor?

From Dev

How to return child class's object when function return type is parent class?

From Dev

Typecast/Downcast Parent Object to Inherited Child Class

From Dev

Initializing an object in a parent class with the type of the child

From Dev

Access child model class from parent model class object in Odoo8

From Dev

is there any way to call parent class method from child class object in java without modifying methods

From Dev

python: How to call a function of parent class from child class object against MRO

From Dev

Initialize the members of parent class with an instance of that class from the child class

Related Related

  1. 1

    How do I pass a parent id as an fk to child object's ModelForm using generic class-based views in Django?

  2. 2

    Typecasting an object from parent class to child

  3. 3

    Can I create object of child class using method from parent class

  4. 4

    Can I create object of child class using method from parent class

  5. 5

    Calling method of child class from object of parent class in C++

  6. 6

    public object from parent class is null in child class xaml

  7. 7

    Why is it necessary for parent class to have a default constructor if we do not create child class object in child class?

  8. 8

    Assigning a child class object to an object of the parent class

  9. 9

    Assigning a child class object to an object of the parent class

  10. 10

    How to call a Parent Class's method from Child Class in Python?

  11. 11

    how to simply access property of parent class from child class by making object of child class

  12. 12

    Create a child instance from parent class in AngularJS+Typescript

  13. 13

    How to check if child object is from specific class (and not parent) (Java)

  14. 14

    Get child class from parent

  15. 15

    Appending values from a child class to parent class

  16. 16

    Return child class from parent class

  17. 17

    Accessing child class prototype from parent class

  18. 18

    Use child's inner class from parent template

  19. 19

    Return child class from parent's static method

  20. 20

    How to use parent project's class from a child project

  21. 21

    Why is it allowed for a parent class object to be instantiated using a child class's constructor?

  22. 22

    Why is it allowed for a parent class object to be instantiated using a child class's constructor?

  23. 23

    How to return child class's object when function return type is parent class?

  24. 24

    Typecast/Downcast Parent Object to Inherited Child Class

  25. 25

    Initializing an object in a parent class with the type of the child

  26. 26

    Access child model class from parent model class object in Odoo8

  27. 27

    is there any way to call parent class method from child class object in java without modifying methods

  28. 28

    python: How to call a function of parent class from child class object against MRO

  29. 29

    Initialize the members of parent class with an instance of that class from the child class

HotTag

Archive