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

Question:

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’

Asked By: parmer_110

||

Answers:

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.

Answered By: Adrien Riaux
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.