Search code examples
pythonpython-import

Using different classes (one imported and one defined) with the same name in a module


from .models import User, AuctionListing, Comment, Bids, Category, Watchlist, Activities, Winners

and

class Comment(forms.Form):
    comment = forms.CharField(label="", widget=forms.Textarea(attrs={
        'placeholder': 'Comment', 'class': 'listing_textarera'
    }))

Class name is Comment.

I have imported from .models and individual definition as above.

Here is in views.py module.

This two Comment class are different.

How can I using each one (Comment is imported from models.py or Class is defined here) separately?

May I refer each one individually?

May experience is here:

c = Comment(message=message, user=request.user, listing=listing)

Which throws an error:

got an unexpected keyword argument 'message'


Solution

  • You can use the as keyword to import the class Comment with a different name, like:

    import .models.Comment as MComment
    

    It will avoid all the misinterpretation using them.